Automatic assembly is available in the following ways:
1 ByName The name of the ID and the name of the property, to ensure that the name of the attribute in the bean instance is the same as the ID name of the assembly.
2 Bytype determines the assembled bean by type, but when there are multiple types of matching beans, an error occurs.
3 Contructor When constructing the injection, the assembly method is used, and the effect is as bytype.
4 AutoDetect first try to use constructor to automatically assemble the line. If it fails, try using Bytype to automatically assemble the line again. (This test, 3.0.5 version is not available, do not know whether it was removed.) )
The configuration file using ByName mode is as follows:
<?XML version= "1.0" encoding= "UTF-8"?><BeansXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://www.springframework.org/schema/beans"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd "> <BeanID= "instrument"class= "Com.spring.test.setter.Saxophone"/> <BeanID= "Kenny"class= "Com.spring.test.setter.Instrumentalist"Autowire= "ByName"> < Propertyname= "Song"value= "Jingle Bells" /> < Propertyname= "Age"value= "+" /> </Bean></Beans>
The configuration file with Bytype is as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://www.springframework.org/schema/beans" xsi:schemalocation= "http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> <bean id=" Test2 " class= "Com.spring.test.setter.Saxophone"/> <bean id= "test1" class= "Com.spring.test.setter.Saxophone" Primary= "true"/> <!--defaults to False-- <bean id= "Kenny" class= "Com.spring.test.setter.Instrumentalist" Autowire= "Bytype" > <property name= "song" Value= "Jingle Bells"/> <property name= "age" value= "25" /> </bean></beans>
Minimize Spring XML configuration