-1

In my Spring application I use the line

@Autowired
private transient EntityService entityService;

at some places successful to get get the EntityService. But not so in the AbstractHttpMessageConverter I write to give CSV responses (which works generally)

public class SearchResultCsvConverter extends AbstractHttpMessageConverter<SearchResult> {

    public SearchResultCsvConverter() {
        super(new MediaType("text", "csv"));
    }

    @Autowired
    private transient EntityService entityService;

Registration:

@ComponentScan(...)
@Configuration
@EnableWebMvc
@EnableAsync
@EnableTransactionManagement
@EnableSpringConfigured
@PropertySource("classpath:config/application.properties")
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {

    @Inject
    private Environment environment;

    @Autowired
    private transient EntityService entityService;

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new SearchResultCsvConverter());
    }

The SearchResultCsvConverter as such works, also EntityService is present in the ApplicationConfiguration, but in SearchResultCsvConverter it's null. Any hints where I might find the mistake oder what to look after are much appreciated!

I use Spring 4.3.4.

-- edit 1

I tried @service for the SearchResultCsvConverter as well - same result.

-- edit 2: solution thanks to @Radu Pop

public class ApplicationConfiguration extends WebMvcConfigurerAdapter {

    @Inject
    private Environment environment;

    @Autowired
    private transient EntityService entityService;

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
         final SearchResultCsvConverter searchResultCsvConverter = new SearchResultCsvConverter();
         searchResultCsvConverter.setEntityService(entityService);
         converters.add(searchResultCsvConverter);

and in the Converter

private transient EntityService entityService;

public SearchResultCsvConverter() {
    super(new MediaType("text", "csv"));
}

public void setEntityService(EntityService entityService) {
    this.entityService = entityService;
}

Doesn't look so elegant to me, but works fine.

Paflow
  • 2,030
  • 3
  • 30
  • 50

1 Answers1

0

As I see from your code, the converter class you have written is not considered a bean in spring application context. This is because the list itself that is defined in you application configuration class is a bean, but no so the elements from it.

Therefore, you have the possibility to make the simple converter be also considered a bean. You could do that by using @Configurable approach, but you need to enable spring AOP possibilities, or you could just create in you application config class a method that will return a bean of that type and carry out the instantiation there. This method will be referenced in the one returning the list, to proper populate the converters with your custom bean.

raduone
  • 191
  • 1
  • 10