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 findmaster Bean 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. |