Default-autowire = "X"
X has four options: byname, bytype, constructor, and autodetect.
1. byname:
Service. Java
public class Service
{
Source source;
public void setSource(Source source)
{
this.source = source;
}
}
Applicationcontext. xml
<beans
...
default-autowire="byName">
<bean id="source" class="cn.hh.spring.DBCPSource" scope="prototype"/>
<bean id="service" class="cn.hh.spring.Service" scope="prototype">
</bean>
</beans>
CN. HH. Spring. dbcpsource implements the source interface.
The source attribute is not configured for bean service in XML, but autowire = "byname" is set in beans. In this way, the configuration file is automatically set according to CN. HH. spring. in the service, setsource finds the bean id = "Source" and automatically configures the bean. If the bean is not found, it is not assembled.
Note: The name of byname is the XXXX of setxxxx in Java. It has nothing to do with the source spelling in the source set above. It can be
public class Service
{
Source source1;
public void setSource(Source source1)
{
this.source1 = source1;
}
}
2. bytype:
Service. Java is the same as above
Applicationcontext. xml
<beans
...
default-autowire="byType">
<bean id="dbcpSource" class="cn.hh.spring.DBCPSource" scope="prototype"/>
<bean id="service" class="cn.hh.spring.Service" scope="prototype">
</bean>
</beans>
Setsource is not configured, and autowire is changed to "bytype". The configuration file will find the bean that implements the source interface. HereCN. HH. Spring. dbcpsource implements the source interface, so it is automatically assembled. If not found, it is not assembled.
If two beans in the same configuration file implement the source interface, an error is returned.
The type here refers to the type of the parameter in setsource (source.
3. constructor:
Try to find one or more beans in the container that are consistent with the constructor parameters of the bean to be automatically assembled. If not, an exception is thrown.
4. autodetect:
First try to use constructor for automatic assembly, and then use bytype method.