public class Autowiringdao { private String Daoname; public void Setdaoname (String daoname) { Daoname; public String Getdaoname () { return Daoname; public void say (String word) {System.out.println (daoname + "===autowiringdao===" + word ); }}
Public classAutowiringservice {PrivateAutowiringdao AutoWiringDao1; PrivateAutowiringservice () {System.out.println ("No parameter construction method"); } PrivateAutowiringservice (Autowiringdao autoWiringDao3) {autoWiringDao1=AutoWiringDao3; System.out.println ("parameter construction method = = =" +autowiringdao1.getdaoname ()); } Public voidsetAutoWiringDao1 (Autowiringdao autowiringdao) {autoWiringDao1=Autowiringdao; } Public voidsay (String word) {Autowiringdao1.say (word); }}
The above is the required class Autowiringservice and class Autowiringdao.
1, ByName
Spring-autowiring.xml file
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.0.xsd "Default-autowire= "byname" > <BeanID= "Autowiringdao"class= "Com.wangzhu.spring.autowiring.AutoWiringDao"> < Propertyname= "Daoname"value= "Autowiringdao"/> </Bean> <BeanID= "AutoWiringDao1"class= "Com.wangzhu.spring.autowiring.AutoWiringDao"> < Propertyname= "Daoname"value= "AutoWiringDao1"/> </Bean> <BeanID= "AutoWiringDao2"class= "Com.wangzhu.spring.autowiring.AutoWiringDao"> < Propertyname= "Daoname"value= "AutoWiringDao2"/> </Bean> <Beanclass= "Com.wangzhu.spring.autowiring.AutoWiringDao"> < Propertyname= "Daoname"value= "NULL"/> </Bean> <BeanID= "Autowiringservice"class= "Com.wangzhu.spring.autowiring.AutoWiringService"></Bean></Beans>
Test:
Autowiringservice service = Getbean ("Autowiringservice"); Service.say ("This is a Test autowire!");
Output:
Non-parametric construction method AutoWiringDao1===autowiringdao=== This is a test autowire!
Note: Automatic assembly by means of the ByName method,
1), the need for non-parametric construction method;
2), requires a member variable with the same name, and a set method for the variable.
2, Bytype
Only need to change the Spring-autowiring.xml file
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.0.xsd "Default-autowire= "bytype" > <Beanclass= "Com.wangzhu.spring.autowiring.AutoWiringDao"> < Propertyname= "Daoname"value= "NULL"/> </Bean> <BeanID= "Autowiringservice"class= "Com.wangzhu.spring.autowiring.AutoWiringService"></Bean></Beans>
Output:
Non-parametric construction method Null===autowiringdao===this is a test autowire!
Note: Automatic assembly by means of the Bytype method,
1), the need for a non-parametric composition method;
2), the same type of bean can only exist one, or will throw an exception;
3), the Set method needs the member variable, its variable name is arbitrary, so in the spring-autowiring.xml only need to specify the Bean class.
3, constructor
Only need to change the Spring-autowiring.xml file
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.0.xsd "Default-autowire= "constructor" > <Beanclass= "Com.wangzhu.spring.autowiring.AutoWiringDao"> < Propertyname= "Daoname"value= "NULL"/> </Bean> <BeanID= "Autowiringservice"class= "Com.wangzhu.spring.autowiring.AutoWiringService"></Bean></Beans>
Output:
The method of constructive ===nullnull===autowiringdao===this is a test autowire!
Note: Automatic assembly by means of the constructor method,
1), need to correspond to the method of constructive, if none throws an exception, of course, if there is no parameter construction method, it will not throw an exception;
2), when there is only one bean of the same type member variable, specify the Bean's class directly;
3), when a bean with more than one member variable of the same type, it is compared with the name of the parameter in the constructor method, and if it is not the same, the parameterless constructor method is called directly.
Auto-assemble "Spring autowire"