Spring 2.5 relies on injection in three ways: 1. Injection through the setter method; 2. Injection through the constructor; 3. Injection through annotations; The first way: inject through the setter method Java code package com.test; public class UserServiceImplement implements IUserService { private IUserDao user; public IUserDao getUser() { return user; } public void setUser(IUserDao user) { this.user = user; } public void saveUser() { 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:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd"> <bean id="userdao" class="com.test.UserDaoImplement"></bean> <bean id="userservice" class="com.test.UserServiceImplement"> <property name="user" ref="userdao"></property> </bean> </beans> Paying attention to this method can also change the XML to the following way: 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:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd"> <bean id="userservice" class="com.test.UserServiceImplement"> <!-- <property name="user" ref="userdao"></property>--> <property name="user"> <bean class="com.test.UserDaoImplement"></bean> </property> </bean> </beans> The difference between the two is that the second method can only be injected into the current bean instance, and cannot be injected into other beans. The injected bean in the first method can be injected into multiple beans, which may cause There are some thread safety issues, so it is better to specify the scope of the injected bean. In addition, through the setter injection, the corresponding injection depends on the property, and there must be a setter method. Test code: Java code package com.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext( "com/test/bean.xml"); IUserService us = (IUserService) ctx.getBean("userservice"); us.saveUser(); } } The second way: injection through the constructor Java code package com.test; public class UserServiceImplement implements IUserService { private IUserDao user; int age; public UserServiceImplement(IUserDao user, int age) { this.user = user; this.age = age; } public void saveUser() { user.addUser(); System.out.println(this.age); } } Xml code <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd"> <bean id="userdao" class="com.test.UserDaoImplement"></bean> <bean id="userservice" class="com.test.UserServiceImplement"> <constructor-arg index="0" type="com.test.IUserDao" ref="userdao"></constructor-arg> <constructor-arg index="1" value="24"></constructor-arg> </bean> </beans> pay attention The index attribute in the <contrucotr-arg> tag can not be specified if the constructor has only one parameter. Its subscript starts from 0, indicating the index of the constructor parameter. If there is a constructor with multiple parameters, the index must be specified. In addition, there is a type attribute, which is used to specify the parameter type of the injected parameter, which must be the same as the parameter type in the constructor. If it is an interface, it is not allowed to pass its implementation class. is an optional attribute; If the parameter type of the constructor is a basic data type, then the ref attribute is not used, but the value attribute is used to set its value, and these data types will be automatically packed and unpacked; Also pay attention to the scope of beans. The test code is the same as above... The third way: injection through java annotations Injecting in this way can be marked with two annotations: @Resource @Autowired The difference between the two: 1. The @Resource annotation is provided by JDK, while the @Autowired annotation is provided by Spring, so the @Autowired annotation will be tightly coupled with Spring, so it is recommended to use the @Resource annotation; 2. @Resource is assembled and injected by name by default. When no bean matching the name is found, the injection will be assembled by type; 3. @Autowired is injected according to the type assembly by default. If you want to transfer the injection according to the name, you need to use it together with @Qualifier; 4. Both @Resource and @Autowired can be used to annotate fields or setter methods. I. Use @Resource annotation for injection Java code package com.test; import javax.annotation.Resource; public class UserServiceImplement implements IUserService { @Resource private IUserDao user; private IUserDao user1; public IUserDao getUser1() { return user1; } @Resource public void setUser1(IUserDao user1) { this.user1 = user1; } public void saveUser() { user.addUser(); System.out.println("user injected successfully"); user1.addUser(); System.out.println("user1 injected successfully"); } } 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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="user" class="com.test.UserDaoImplement"></bean> <bean id="user1" class="com.test.UserDaoImplement"></bean> <bean id="userservice" class="com.test.UserServiceImplement"></bean> </beans> Test class: Java code package com.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext( "com/test/bean.xml"); IUserService us = (IUserService) ctx.getBean("userservice"); us.saveUser(); } } II. Using @Autowired Java code package com.test; import org.springframework.beans.factory.annotation.Autowired; public class UserServiceImplement implements IUserService { @Autowired private IUserDao user; public IUserDao getUser() { return user; } public void setUser(IUserDao user) { this.user = user; } public void saveUser() { user.addUser(); System.out.println("user injected successfully"); } } 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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="user" class="com.test.UserDaoImplement"></bean> <bean id="userservice" class="com.test.UserServiceImplement"></bean> </beans> Test class: Java code package com.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext( "com/test/bean.xml"); IUserService us = (IUserService) ctx.getBean("userservice"); us.saveUser(); } }
Spring 2.5 relies on injection in three ways
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326943433&siteId=291194637
Ranking