依赖注入DI
1,当某个角色(可能是一个Java实例,调用者)需要另一个角色(另一个Java实例,被调用者)的协助时,在 传统的程序设计过程中,通常由调用者来创建被调用者的实例。但在Spring里,创建被调用者的工作不再由调用者来完成,因此称为控制反转;创建被调用者 实例的工作通常由Spring容器来完成,然后注入调用者,因此也称为依赖注入
2,所谓依赖注入,是指程序运行过程中,如果需要调用另一个对象协助时,无须在代码中创建被调用者,而是依赖于外部的注入。Spring的依赖注入对调用者和被调用者几乎没有任何要求,完全支持对POJO之间依赖关系的管理
例题
1,建实体类Address和Student
Student
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String girlFriend;
private Properties info;
public Student() {
}
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 setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public void setGames(Set<String> games) {
this.games = games;
}
public void setGirlFriend(String girlFriend) {
this.girlFriend = girlFriend;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", girlFriend='" + girlFriend + '\'' +
", info=" + info +
'}';
}
}
Address
public class Address {
private String address;
public Address() {
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
2,建配置文件 beans.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--Address-->
<bean id="addr" class="pojo.Address">
<property name="address" value="西安"/>
</bean>
<!--Student-->
<bean id="Student" class="pojo.Student">
<!--常量注入 普通字段-->
<property name="name" value="闪电侠"/>
<!--Bean注入 引用其他bean使用ref-->
<property name="address" ref="addr"/>
<!--数组注入-->
<property name="books">
<array>
<value>西游记</value>
<value>水浒传</value>
<value>红楼梦</value>
<value>三国演义</value>
</array>
</property>
<!--List注入-->
<property name="hobbys">
<list>
<value>代码</value>
<value>电影</value>
<value>音乐</value>
</list>
</property>
<!--Map注入-->
<property name="card">
<map>
<entry key="IdCard" value="666666888888884444"/>
<entry key="银行卡" value="111122223333444"/>
</map>
</property>
<!--Set注入-->
<property name="games">
<set>
<value>王者荣耀</value>
<value>绝地求生</value>
</set>
</property>
<!--Null空值注入-->
<property name="girlFriend">
<null/>
</property>
<!--Properties注入-->
<property name="info">
<props>
<prop key="学号">2019032324</prop>
<prop key="性别">男</prop>
<prop key="姓名">闪电侠</prop>
</props>
</property>
</bean>
</beans>
3,编写测试类
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StudentTest {
@Test
public void Test(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("Student");
System.out.println(student.toString());
}
}
4,运行结果