依赖注入的三种方式
1. 构造器注入
class UserService {
private UserDao userDao;
public UserService(UserDao userDao){
this.userDao = userDao;
}
}
2. setter注入
class Uservice {
private UserDao userDao;
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}
3. 感知接口注入
interface UserDaoAware {
public void injectUserDao(UserDao userDao);
}
class UserService implements UserDaoAware {
private UserDao userDao;
public void injectUserDao(UserDao userDao){
this.userDao = userDao;
}
}
Spring支持前两种方式
Employee
package main.com.lee.e_di; /** * 雇员 * * @author Lynch * */ public class Employee { private Integer age; private String name; private Product product; public void setProduct(Product product) { this.product = product; } public void setAge(Integer age) { this.age = age; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Employee [age=" + age + ", name=" + name + ", product=" + product + "]"; } }
EmployeeInfo
package main.com.lee.e_di; /** * 数据bean * * @author Lynch * */ public class EmployeeInfo { private String name = "周通"; private Integer age = 23; public String getName() { return name; } public Integer peekAge() { return age; } }
Product
package main.com.lee.e_di; /** * 商品 */ public class Product { private String name; private Double price; public Product(String name, Double price) { this.name = name; this.price = price; } @Override public String toString() { return "Product [name=" + name + ", price=" + price + "]"; } }
Student
package main.com.lee.e_di; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Student { private String[] aArr; private List<String> aList; private Set<String> aSet; private Map<String, String> aMap; private Properties properties; public void setProperties(Properties properties) { this.properties = properties; } public void setaArr(String[] aArr) { this.aArr = aArr; } public void setaList(List<String> aList) { this.aList = aList; } public void setaSet(Set<String> aSet) { this.aSet = aSet; } public void setaMap(Map<String, String> aMap) { this.aMap = aMap; } @Override public String toString() { return "Student [aArr=" + Arrays.toString(aArr) + ", aList=" + aList + ", aSet=" + aSet + ", aMap=" + aMap + ", properties=" + properties + "]"; } }
applicationCotext.xml
<!-- 依赖注入的方式 构造器注入 --> <bean id="product" class="main.com.lee.e_di.Product"> <constructor-arg index="0" type="java.lang.String" value="宝马" /> <constructor-arg index="1" type="java.lang.Double" value="1111.1" /> </bean> <!-- 依赖注入的方式 setter方式 --> <bean id="employee" class="main.com.lee.e_di.Employee"> <property name="age" value="23" /> <property name="name" value="成龙" /> <property name="product" ref="product" /> </bean> <!-- P名称空间注入 --> <bean id="employee2" class="main.com.lee.e_di.Employee" p:name="李连杰" p:age="44" p:product-ref="product" /> <!-- SpEL --> <bean id="employeeInfo" class="main.com.lee.e_di.EmployeeInfo" /> <bean id="employee3" class="main.com.lee.e_di.Employee"> <property name="name" value="#{employeeInfo.name}"></property> <property name="age" value="#{employeeInfo.peekAge()}"></property> <property name="product" value="#{product}"></property> </bean> <!-- 集合属性的注入 --> <bean id="student" class="main.com.lee.e_di.Student"> <property name="aArr"> <list> <value>数组值1</value> <value>数组值2</value> </list> </property> <property name="aList"> <list> <value>列表值1</value> <value>列表值2</value> </list> </property> <property name="aSet"> <set> <value>集合值1</value> <value>集合值2</value> <value>集合值2</value> </set> </property> <property name="aMap"> <map> <entry key="key1" value="映射值1"></entry> <entry> <key> <value>key2</value> </key> <value>映射值2</value> </entry> </map> </property> <property name="properties"> <props> <prop key="key1">值1</prop> <prop key="key2">值2</prop> </props> </property> </bean>
Test
package main.com.lee.e_di; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 依赖注入方式小测 * * @author Lynch * */ public class BeanDITest { @Test public void demo1() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); // 构造器注入 Product product = (Product) applicationContext.getBean("product"); System.out.println(product); // setter注入 Employee employee = (Employee) applicationContext.getBean("employee"); System.out.println(employee); // p名称空间注入 Employee employee2 = (Employee) applicationContext.getBean("employee2"); System.out.println(employee2); // SpEL注入 Employee employee3 = (Employee) applicationContext.getBean("employee3"); System.out.println(employee3); // 集合注入 Student student = (Student) applicationContext.getBean("student"); System.out.println(student); } }
out
Product [name=宝马, price=1111.1] Employee [age=23, name=成龙, product=Product [name=宝马, price=1111.1]] Employee [age=44, name=李连杰, product=Product [name=宝马, price=1111.1]] Employee [age=23, name=周通, product=Product [name=宝马, price=1111.1]] Student [aArr=[数组值1, 数组值2], aList=[列表值1, 列表值2], aSet=[集合值1, 集合值2], aMap={key1=映射值1, key2=映射值2}, properties={key2=值2, key1=值1}]