0

The problem:

I have a bean (ConversionService) that needs a collection of Converters to be created. So within my @Configuration class I have a @Bean that is a Collection<Converter> with a specific @Qualifier.

For my ConversionService @Bean, I receive the Converter collection as parameter using my @Qualifier like this:

@Bean
public ConversionService createConversionService(@Qualifier("converters") converters) {
    // here I perform the ConversionService creation
}

This works and is exactly how I want it. But I have several @Configuration classes, and each one shall be able to add something to the Converter collection. I initially though maybe there is a way to implement a method invoked after bean definition is read from @Configuration class. Something like this:

@Configuration
public class MyConfiguration {

    @Autowired
    @Qualifier("converters")
    private Collection<Converter> converters;

    public void init() {
        converters.add(xy);
    }

}

or even

@Configuration
public class MyConfiguration {

    public void init(@Qualifier("converters") Collection<Converter> converters) {
        converters.add(xy);
    }

}
Francisco Spaeth
  • 23,493
  • 7
  • 67
  • 106

1 Answers1

1

You should be able to add something to your converters in your @Configuration annotated class using the @PostConstruct annotation.

@Configuration
public class MyConfiguration {

    @Autowired
    @Qualifier("converters")
    private Collection<Converter> converters;

    @PostConstruct
    public void init() {
        converters.add(xy);
    }

}
Alex
  • 25,147
  • 6
  • 59
  • 55
  • Is there a way to do it using just spring classes? @PostConstruct is a javax annotation. – Francisco Spaeth Oct 31 '12 at 07:39
  • perfect, I got it implementing org.springframework.beans.factory.InitializingBean Thanks for your help Alex! – Francisco Spaeth Oct 31 '12 at 07:40
  • @JavaMentor : can you post your answer? – Nandkumar Tekale Oct 31 '12 at 07:44
  • @NandkumarTekale: I solved it implementing something like: `@Configuration public class CoreConfiguration implements InitializingBean {`, the approach is very much like my first example of `@Configuration` class, but instead of method `init` I shall implement `public void afterPropertiesSet() throws Exception {`. This answer solved the problem using `javax.annotation`, and in fact will have the same result. – Francisco Spaeth Oct 31 '12 at 08:06