Automatic spring bean autowire assembly

Source: Internet
Author: User

Autowire = "byname" automatically assembles beans with the same attributes as bean IDs. For example, there are two beans,

<Bean id = "userdao" class = "com. springdemo. Dao. admindao"> </bean>
<Bean id = "userservice3" autowire = "byname" class = "com. springdemo. Service. userservice"> </bean>

The userservice class has a userdao attribute:

Private iuserdao userdao;

Public iuserdao getuserdao (){
Return userdao;
}

Public void setuserdao (iuserdao userdao ){
This. userdao = userdao;
}

In this way, when the bean userservice3 is created, byname will find the bean userdao and setter will be injected into the bean instance userservice3.

If setter injection is set in userservice3, as follows:

<Bean id = "clientdao" class = "com. springdemo. Dao. clientdao"> </bean>

<Bean id = "userservice2" autowire = "byname"
Class = "com. springdemo. Service. userservice">
<Property name = "userdao">
<Ref bean = "clientdao"/>
</Property>
</Bean>

At this time, there are both autowire and property, so the setter of property will overwrite the setter injection of autowire. At this time, userdao in the userservice class is actually COM. springdemo. dao. clientdao type, not com. springdemo. dao. admindao

 

 

Table 3.2. autowiring Modes

Mode Description
No  
Byname

Automatic Assembly Based on Attribute names. This option checks the container, finds the bean exactly the same as the property by name, and automatically assembles it with the property. For example, in the bean definition, set autowire to by name, and the bean containsMasterAttribute (both provideSetmaster (..)Method), spring will findmasterBean definition, and use it to assemble the attributes to the master.

Bytype

If a bean of the same type as the specified property exists in the container, it is automatically assembled with the property. If there are multiple beans of this type, an exception will be thrown and it indicates that the bean cannot be used.BytypeMethod for Automatic Assembly. If no matching bean is found, nothing happens and attributes are not set. If you do not want this, you can setdependency-check="objects"Let spring throw an exception.

Constructor

AndBytypeThe difference is that it is applied to the constructor parameters. If no bean of the same type as the constructor parameter is found in the container, an exception is thrown.

Autodetect

Through the introspection mechanism of bean classesConstructorOrBytypeMethod for Automatic Assembly. If the default constructor is foundBytypeMethod.

 

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.