@Configuration class, pass @Bean to other @Bean

Viewed 84

Which is the best way to pass/use a @Bean type present in the same configuration class to another @Bean? For example if I have the current situation:

@Configuration
public ConfigurationSample {

     @Bean("first")
     public CustomType beanA(){

     }

     @Bean("four")
     public CustomType beanD(){

     }

    @Bean("second")
    public OtherCustomType beanB(@Qualifier("first") CustomType bean){
         //Use bean
    }

    @Bean("third")
    public OtherCustomType  beanC(){
       CustomType  bean = beanA();
       ....
       ....
    }

}

Should I use the @Qualifier annotation or i can directly call the bean or it's equivalent and why?

EDIT

Reading responses I added beanD() missing before (sorry!) to make more clear the need of @Qualifier annotation.

3 Answers

Use @Qualifier if you have 2 implementation of the same bean;

@Configuration
public ConfigurationSample {

     @Bean("first")
     public CustomType beanA(){
       //
     }

    @Bean("second")
    public CustomType beanB(){
       //
    }

    @Bean("third")
    public OtherCustomType beanC(@Qualifier("first") CustomType bean){
       //Use bean
    }    
}

In your case you don't need to use @Qualifier

EDITED after the question edit introducing beanD.

Let's make it clear:

  • CustomType beanA doesn't depend on anything
  • OtherCustomType beanB depends on CustomType beanA
  • OtherCustomType beanC depends on CustomType beanA
  • CustomType beanD doesn't depend on anything

Therefore all you need to do is:

  • To distinguish OtherCustomType beanB from OtherCustomType beanC
  • To distinguish CustomType beanA from CustomType beanD

... as long as they are of the same type. Autowiring CustomType beanA is hence not safe without a qualifier. You have two choices:

  1. Using @Qualifier to autowire a certain bean.
  2. Annotating beanA with @Primary to define the precedence in the autowiring.

The following snippet is the example of the first way (using @Qualifier):

@Configuration
public ConfigurationSample {

    @Bean("beanA")
    public CustomType beanA() { 
         /** CODE **/ 
    }

    @Bean("beanB")
    public OtherCustomType beanB(@Qualifier("beanA") CustomType beanA) {
         /** USE beanA HERE **/
    }

    @Bean("beanC")
    public OtherCustomType beanC(@Qualifier("beanA") CustomType beanA) {
         /** USE beanA HERE **/
    }

    @Bean("beanD")
    public CustomType beanD(){
        /** CODE **/ 
    }
}

To call the method beanA() inside the beanB and beanC body instead of autowiring is safe too.

When create more than one bean of the same type and want to wire only one of them with a property. In such cases, you can use the @Qualifier annotation along with @Autowired to remove the confusion by specifying which exact bean will be wired.

As in the xml

<!-- Definition for student1 bean -->
   <bean id = "student1" class = "com.test">
      <property name = "name" value = "alpha" />
      <property name = "age" value = "11"/>
   </bean>

<!-- Definition for student2 bean -->
  <bean id = "student2" class = "com.test">
      <property name = "name" value = "beta" />
      <property name = "age" value = "2"/>
  </bean>

In the class

 public class Profile {
       @Autowired
       @Qualifier("student1")
       private Student student;
    }
Related