3

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.

1
  • If there is just a single instance, don't use an @Qualifier or just call the method from beanb() method. Either will work.
    – M. Deinum
    Commented Nov 10, 2020 at 9:55

3 Answers 3

3

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

2
  • The beanB is of OtherCustomType type. You don't need to qualify the beanA. Commented Nov 10, 2020 at 10:48
  • 1
    Actually I try to explain when @Qualifier should be used but thanks for correction!
    – Ömer
    Commented Nov 10, 2020 at 12:50
2

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.

1

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;
    }

Not the answer you're looking for? Browse other questions tagged or ask your own question.