In spring, 5 automatic assembly mode is supported.
- no– By default, automatic configuration is manually set by the "ref" property
- byname– is automatically assembled according to the property name. If the name of a bean is the same as the name of another bean property, it will be self-assembled.
- The bytype– is automatically assembled by data type. If the data type of a bean is the data type of another Bean property, it is compatible and automatically assembled.
- constructor– the Bytype mode of the constructor parameter.
- autodetect– If a default constructor is found, use "Automatic assembly construction"; Otherwise, use automatic assembly by type.
Example:
The Customer and person objects are automatically assembled for demonstration.
public class Customer { private person person; public Customer (person person) { this . Person = person; public void Setperson" { this . Person =// ... }
Public class Person { //...}
1. auto-wiring ' No ' This is the default mode and you need to connect the bean through the ' ref ' attribute.
class= ' Com.yiibai.common.Customer ' > <property name= ' person ' ref= ' person '/> </bean > class= "Com.yiibai.common.Person"/>
2. auto-wiring ' ByName '
Auto assemble by attribute name. In this case, because the name of the "person" Bean is the same as the property ("person") name of the "Customer" bean, spring will automatically assemble it through the setter method – "Setperson" “.
class= "Com.yiibai.common.Customer" autowire= "byname"/> class= "Com.yiibai.common.Person "/>
3. auto-wiring ' Bytype '
The bean is automatically assembled by the data type of the attribute. In this case, because the data type in the "person" Bean is the same as the data type of the "customer" Bean's property (Person object), spring automatically assembles it automatically through the setter method. – "Setperson (person person)".
class class= "Com.yiibai.common.Person"/>
4. Auto-wiring ' constructor '
The bean is automatically assembled by attributes by the data type of the constructor parameter. In this case, because the data type of the "person" Bean is the same as the data type of the constructor parameter of the "Customer" Bean's property (Person object), spring is automatically assembled by constructing the method – "Public customer ( Person) ".
class= "Com.yiibai.common.Customer" autowire= "constructor"/> class= " Com.yiibai.common.Person "/>
5. auto-wiring ' AutoDetect '
If there is a default construction method, it is automatically assembled by construct, otherwise it is automatically assembled using the Bytype way. For spring2.5, the spring3.0 test does not pass, it is estimated to be obsolete.
Spring Autowire Automatic Assembly