Autowired does not throw exceptions for multiple beans of the same type

I am working on a basic @Autowired program, where I have 2 classes Alpha and Beta . Here Alpha has a dependency on Beta using @Autowired .

In the spring configuration file, more than 1 bean was created for the Beta class, so I expected an exception from spring when it tries to insert a dependency in the Alpha class, since there are 2 beta versions of beans instead of 1. But in my program I do not get any exceptions, it works great.

Here is my code:

Alpha.java

 public class Alpha { @Autowired private Beta beta; public Alpha() { System.out.println("Inside Alpha constructor."); } @Override public String toString() { return "Alpha [beta=" + beta + "]"; } } 

Beta.java

 public class Beta { public Beta() { System.out.println("Inside Beta constructor."); } @Override public String toString() { return "This is Beta"; } } 

spring -config.xml

 <beans> <context:annotation-config/> <bean id="alpha" class="Alpha"> </bean> <bean id="beta" class="Beta"> </bean> <bean id="beta1" class="Beta"> </bean> <bean id="beta2" class="Beta"> </bean> </beans> 

The main program:

 public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Alpha alpha = (Alpha) context.getBean("alpha"); System.out.println(alpha); } 

This is the conclusion:

 Inside Alpha constructor. Inside Beta constructor. Inside Beta constructor. Inside Beta constructor. Alpha [beta=This is Beta] 
+4
source share
3 answers

It is automatically updated by name. You have three beans of type Beta1 named beta , Beta1 and beta2 . Your field is called beta . Spring will use this as a hint to find the appropriate bean.

If you have specified your field

 @Autowired private Beta whatever; 

there would be no (useful) hint of Spring, and he could not choose the appropriate bean.

This is not entirely obvious in the Spring documentation, you should draw conclusions from different sections.

From the notes in the chapter regarding @Autowired

If you intend to express an injection caused by annotations by name, do not use @Autowired , even if you can technically refer to the bean value using @Qualifier values. Instead, use the JSR-250 @Resource annotation , which is semantically defined to identify a specific target component by its unique name, with the declared type does not matter for the matching process.

For @Qualifier values ​​(or rather, lack of @Qualifier )

For reciprocal matching, the bean name is considered the default classifier value

Then from a chapter in @Resource

If the name is not specified [in the @Resource annotation] explicitly, the default name comes from the field name or installation method. In the case of a field, a field name is required ; in the case of the setter method, the bean property is required.

+6
source

Here is a very good summary of the Spring bean resolution mechanism using @Autowired , @Inject and @Resource annotations.

Since the default bean name is the name of the field, a bean beta is entered.

0
source

@ Automatic beta beta is looking for a bean named beta. It is present in the spring bean factory. Therefore, there will be no such problem.

0
source

All Articles