@Autowired By default is injected according to Bytype, but when Bytype way to find a number of matching beans, and how to deal with it?
After some code testing, I found that autowired default first press Bytype, if found to find more than one bean, then, in accordance with the byname way, if there are more than one, the exception is reported.
Example:
@Autowired
Private Examusermapper Examusermapper; -Examusermapper is an interface
1. Spring first look for a bean of type Examusermapper
2. If present and unique, then OK;
3. If not unique, in the result set, look for the bean named Examusermapper. Because the bean name is unique, you should be able to determine if there are any beans that satisfy the requirements.
@Autowired也可以手动指定按照byName方式注入,使用@Qualifier标签,例如:
@Autowired
()
@Qualifier
(
"baseDao"
)
Because the bean's name is unique, theoretically byname will be faster, but spring is injected by default using Bytype, which makes me confused and not sure which one is really fast. Concrete to the actual application, the feeling difference is not big, see everybody's habit Another note: @Resource (this annotation belongs to the Java EE) label, by default is injected according to ByName Way
Java Learning (vii) @autowired injection rules for __spring