The automatic assembly properties of the bean simplifies the configuration of the XML file.
The automatic assembly properties of the bean are divided into four types:
1.byName
2.byTyoe
3.constructor
4. AutoDetect
ByName:
It finds the ID of the bean in the configuration file or automatically assembles the bean with the same name as the member attribute in this bean, so no more peoperty tags are added to this bean
Example: There are two classes
Public Class person{
}
Public Class customer{
private person p;
Public Setperson (person PS) {
This.p=ps;
}
}
The traditional XML configuration is
<bean id= "P" class= "com.***. Person "/>
<bean id= "Customer" class= "com.***. Customer ">
<property name= "P" ref= "P" ></property>//The (ref) p is instantiated to the Bean's (name) P property to achieve the purpose of Setperson in the method
</bean>
The automatic assembly method of ByName is
<bean id= "P" class= "com.***. Person "/>
<bean id= "Customer" class= "com.***. Customer "autowrite=" byname "/>//Because instances of id=p classes are aliases exactly and com.***. The attribute p of the customer class has the same member variable name so it is automatically assembled into the customer's bean.
Bytype:
The same way: only he searches for the type of member and the type of a bean in the configuration file is automatically assembled, but one thing to be aware of: if multiple beans of the same type will throw an exception, the processing method is. Set the property of the bean that does not need to be loaded primary= "false" (cancel preferred) or autowriter-candidate= "false" (exclude)
Constructor
Try to find one or more beans in the container that match the constructor parameters of the bean that needs to be automatically assembled, and throw an exception if not found
AutoDetect:
First try to use constructor to move the assembly and then use the Bytype mode.
Default Automatic assembly settings:
If you want to apply the same automatic assembly policy to all the beans it creates to simplify configuration, you can add the Default-autowire property on the root element <beans>
<beans default-autowire= "ByName" > ...................</beans>
Automatic assembly properties for Spring Beans