依赖注入(DI)
- 依赖注入(Dependency Injection,DI)。
- 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
- 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .
构造器注入
我们在之前的03已经详细讲过了
settet注入 (重点)
要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is .
pojo类
public class Address {
private String address;
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
public void setName(String name) {
this.name = name;
}
public void setAddress(Address address) {
this.address = address;
}
public void setBooks(String[] books) {
this.books = books;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public void setGames(Set<String> games) {
this.games = games;
}
public void setWife(String wife) {
this.wife = wife;
}
public void setInfo(Properties info) {
this.info = info;
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--常量注入-->
<bean id="addr" class="com.cong.pojo.Address">
<property name="address" value="耀华里***"/>
</bean>
<bean id="cong" class="com.cong.pojo.Student">
<property name="name" value="cong"/>
<!--bean注入,用ref-->
<property name="address" ref="addr"/>
<!--数组注入-->
<property name="books">
<array>
<value>红楼梦</value>
<value>水浒传</value>
<value>西游记</value>
<value>三国演义</value>
</array>
</property>
<!--list注入-->
<property name="hobbies">
<list>
<value>听歌</value>
<value>码字</value>
<value>看电影</value>
</list>
</property>
<!--map注入-->
<property name="card">
<map>
<entry key="idCard" value="4408*******************"/>
<entry key="countCard" value="6201***************"/>
<entry key="studentCard" value="201*****"/>
</map>
</property>
<!--set注入-->
<property name="games">
<set>
<value>lol</value>
<value>魔兽</value>
<value>刀塔</value>
</set>
</property>
<!--null注入-->
<property name="wife">
<null/>
</property>
<!--properties注入-->
<property name="info">
<props>
<prop key="姓名">rainbow</prop>
<prop key="学号">2014****</prop>
<prop key="性别">female</prop>
</props>
</property>
</bean>
</beans>
测试类
public class TestDI {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student cong = (Student) context.getBean("cong");
System.out.println(cong.toString());
}
}
结果
Student{
name='cong',
address=Address{address='耀华里***'},
books=[红楼梦, 水浒传, 西游记, 三国演义], hobbys=[听歌, 码字, 看电影],
card={idCard=4408*******************,
countCard=6201***************,
studentCard=201*****},
games=[lol, 魔兽, 刀塔],
wife='null',
info={学号=2014****, 性别=female, 姓名=rainbow}
}
扩展,命名空间注入
pojo类
package com.cong.pojo;
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
beans.xml
注意要导入约束
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.cong.pojo.User" p:name="cong" p:age="3"/>
<!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
<bean id="user2" class="com.cong.pojo.User" c:name ="cong2" c:age="3"/>
</beans>
测试类
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) context.getBean("user");
System.out.println(user.toString());
User user2 = (User) context.getBean("user2");
System.out.println(user2.toString());
}