6. DI 依赖注入
6.1 构造器注入
**
constructor-arg : 使用有参构造创建对象
<bean id="user" class="com.javacto.pojo.User" >
<!-- 第一种:索引下标赋值 -->
<!-- index: 形参的位置,从0开始 给value: 给这个下标的形参赋值-->
<constructor-arg index="0" value="使用有参构造,index"/>
</bean>
<bean id="user" class="com.javacto.pojo.User" >
<!-- 第二种:type -->
<constructor-arg type="java.lang.String" value="使用了有参构造,type"/>
</bean>
<bean id="user" class="com.javacto.pojo.User" >
<!-- 第三种: name 这种使用最方便 -->
<constructor-arg name="str" value="使用了有参构造,name" />
</bean>
6.2 Set 方式注入
- 依赖注入:set注入
- 依赖:bean对象的创建依赖于容器!
- 注入:bean对象中的所有属性,由容器来注入!
【环境搭建】
- 复杂类型:
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
- 真实测试对象
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String, String> cards;
private Set<String> games;
private String wife;
private Properties properties;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCards() {
return cards;
}
public void setCards(Map<String, String> cards) {
this.cards = cards;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Student{");
sb.append("name='").append(name).append('\'');
sb.append(", address=").append(address);
sb.append(", books=").append(Arrays.toString(books));
sb.append(", hobbys=").append(hobbys);
sb.append(", cards=").append(cards);
sb.append(", games=").append(games);
sb.append(", wife='").append(wife).append('\'');
sb.append(", properties=").append(properties);
sb.append('}');
return sb.toString();
}
}
- beans.xml
<bean id="student" class="com.javacto.pojo.Student">
<!-- name : 类属性的set方法 value : 只能是基本数据类型和String -->
<!-- 第一种: 普通值的注入:value -->
<property name="name" value="eric"/>
</bean>
- 测试类:
public class TestStudent {
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println("name = "+ student.getName());
}
}
完整的set注入方式:
<bean id="address" class="com.javacto.pojo.Address"/>
<bean id="student" class="com.javacto.pojo.Student">
<!-- name : 类属性的set方法 value : 只能是基本数据类型和String -->
<!-- 第一种: 普通值的注入:value -->
<property name="name" value="eric"/>
<!-- 第二种: 引用类型注入, ref -->
<property name="address" ref="address"/>
<!-- 第三种: 数组, array -->
<property name="books" >
<array>
<value>西游记</value>
<value>红楼梦</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<!-- 第四种: 集合, list -->
<property name="hobbys">
<list>
<value>唱</value>
<value>跳</value>
<value>rap</value>
</list>
</property>
<!-- 第五种: map-->
<property name="cards">
<map>
<entry key="身份证" value="12345678"/>
<entry key="银行卡" value="12345678"/>
</map>
</property>
<!-- 第六种:set -->
<property name="games">
<set>
<value>LOL</value>
<value>CrossFire</value>
</set>
</property>
<!-- 第七种: null 注入 -->
<property name="wife">
<null></null>
</property>
<!-- 第八种:properties -->
<property name="properties">
<props>
<prop key="学号">12424</prop>
<prop key="ID">001</prop>
</props>
</property>
</bean>
测试代码:
@Test
public void test2() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student);
// 输出结果:
/*Student{
name='eric',
address=com.javacto.pojo.Address@376b4233,
books=[西游记, 红楼梦, 水浒传, 三国演义],
hobbys=[唱, 跳, rap],
cards={身份证=12345678, 银行卡=12345678},
games=[LOL, CrossFire],
wife='null',
properties={学号=12424, ID=001}
}*/
}
6.3 其它方式
-
写一个实体类User
public class User { private String name; private Integer age; public User() { } public User(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { final StringBuilder sb = new StringBuilder("User{"); sb.append("name='").append(name).append('\''); sb.append(", age=").append(age); sb.append('}'); return sb.toString(); } }
-
userbeans.xml
- 使用命名空间注入,需要添加约束:
<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- P命名空间注入:作用相当于property --> <bean id="user" class="com.javacto.pojo.User" p:name="eric" p:age="123"/> <!-- C命名空间注入:作用相当于construct-args --> <bean id="user2" class="com.javacto.pojo.User" c:name="james" c:age="122"/> </beans>
-
测试:
@Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml"); User user = context.getBean("user", User.class); User user2 = context.getBean("user2", User.class); System.out.println(user); System.out.println(user2); // 输出结果: /* User{name='eric', age=123} User{name='james', age=122} */ }