1 Injected by setter method
Bean class:
Packagecom.test; Public classUserserviceimplementImplementsIuserservice {PrivateIuserdao User; PublicIuserdao GetUser () {returnuser; } Public voidsetUser (Iuserdao user) { This. user =user; } Public voidSaveuser () {user.adduser (); } }
XML code:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:p= "http://www.springframework.org/schema/p"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation="http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp//Www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><bean id= "Userdao"class= "Com.test.UserDaoImplement"/> <bean id= "UserService"class= "Com.test.UserServiceImplement" > <property name= "User" ref= "Userdao"/> </bean></beans>
Note: Setter mode injection, corresponding to the injection dependency property, must have setter method.
2 Injection by construction method
Bean class:
Packagecom.test; Public classUserserviceimplementImplementsIuserservice {PrivateIuserdao User; intAge ; PublicUserserviceimplement (Iuserdao user,intAge ) { This. user =user; This. Age =Age ; } Public voidSaveuser () {user.adduser (); System.out.println ( This. Age); } }
XML code:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:p= "http://www.springframework.org/schema/p"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation="http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp//Www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><bean id= "Userdao"class= "Com.test.UserDaoImplement"/> <bean id= "UserService"class= "Com.test.UserServiceImplement" > <constructor-arg index= "0" type= "Com.test.IUserDao" ref= "Userdao" ></ Constructor-arg> <constructor-arg index= "1" value= "></constructor-arg> </bean></beans" >
Note:
- For the index attribute in the <construcotr-arg> tag, if the constructor method has only one parameter, it can be unspecified, and its subscript starts at 0, which indicates the index of the constructor parameter, if the constructor has more than one parameter, the index must be specified. In addition to the type attribute, type is an optional attribute that specifies the parameter type of the parameter being injected, and must be consistent with the type of the parameter in the constructor, and, if it is an interface, it should not be passed to its implementation class.
- If the parameter type of the constructor is the basic data type, then the ref attribute is not used, and the Value property is set to its values, and the data types are automatically packaged and unpacked;
- Be aware of the bean's scope problem.
- The Bean class to be injected must have a construction method.
3 Injecting with annotations
- @Resource
- @Autowired
The difference:
- @Resource annotations are provided by the JDK, and @autowired annotations are provided by spring, so @autowired annotations are tightly coupled with spring, so it is recommended to use @resource annotations
- @Resource By default is assembled by name, and when no bean matching the name is found, the injection is assembled by type
- @Autowired is injected by type assembly by default, and if you want to transpose the injection by name, you need to use it in conjunction with @qualifier
- Both @Resource and @autowired can be used to label fields or setter methods
@Resource Injection Mode code:
Packagecom.test; ImportJavax.annotation.Resource; Public classUserserviceimplementImplementsIuserservice {@ResourcePrivateIuserdao User; PrivateIuserdao user1; PublicIuserdao GetUser1 () {returnuser1; } @Resource Public voidSetUser1 (Iuserdao user1) { This. User1 =user1; } Public voidSaveuser () {user.adduser (); System.out.println ("User injection succeeded"); User1.adduser (); System.out.println ("User1 injection Success"); } }
XML file:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:p= "http://www.springframework.org/schema/p"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation="http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp//Www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><context:annotation-config/> <bean id= "User"class= "Com.test.UserDaoImplement"/> <bean id= "user1"class= "Com.test.UserDaoImplement"/> <bean id= "UserService"class= "Com.test.UserServiceImplement"/> </beans>
@Autowired Injection Mode code:
Packagecom.test; Importorg.springframework.beans.factory.annotation.Autowired; Public classUserserviceimplementImplementsIuserservice {@AutowiredPrivateIuserdao User; PublicIuserdao GetUser () {returnuser; } Public voidsetUser (Iuserdao user) { This. user =user; } Public voidSaveuser () {user.adduser (); System.out.println ("User injection succeeded"); } }
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"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-2.5.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-2.5.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.0.xsd "> <Context:annotation-config/> <BeanID= "User"class= "Com.test.UserDaoImplement"/> <BeanID= "UserService"class= "Com.test.UserServiceImplement"/> </Beans>
How the IOC container is injected in spring