Java Learning (vii) @autowired injection rules for __spring

Source: Internet
Author: User

@Autowired is injected by default by Bytype, but when the Bytype method finds multiple compliant beans, it is handled.


After some code testing, I found that autowired by default first by pressing Bytype, if found more than one bean, then, in accordance with the ByName method, if there are more than, then reported an exception.

Example:

@Autowired
Private Examusermapper Examusermapper; -Examusermapper is an interface


1. Spring first find a bean of type Examusermapper

2. If present and unique, OK;

3. If not unique, in the result set, look for a bean with name Examusermapper. Because the name of the bean is unique, it should be possible to determine if there is a bean that satisfies the requirement.


@Autowired can also manually specify ByName injection, using @qualifier tags, such as @Autowired () @Qualifier ("Basedao")
Because the name of the bean is unique, theoretically the byname will be faster, but spring defaults to injecting in Bytype, which makes me confused and not sure which one is really fast. Concrete to the actual application, the feeling is not very different, see everyone's habit
Note: The label of the @Resource (this annotation belongs to the Java EE) is injected by default in byname manner

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.