Java Learning (vii) @autowired injection rules for __spring

Source: Internet
Author: User

@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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.