1. 依赖注入(DI: Dependency Injection)
- Spring框架核心IOC的具体实现。
在编写程序时, 通过控制反转, 把对象的创建交给Spring, 但是代码中不可能出现没有依赖的情况,IOC解耦只是降低他们的依赖关系, 不会消除。 例如: 业务层仍会调用持久层的方法。
那这种业务层和持久层的依赖关系, 在使用Spring之后就让Spring来维护了,简单来说,就是等框架把持久层对象传入业务层, 而不用我们自已去获取。
2. 注入的方式
- 构造方法注入:
<constractor-arg index="" name="" ref="">
index: 参数索引位置, 指定了index可以不写name。
name: 参数名称, 跟构造器传入的参数名称保持一致。
ref: 引用对象。
value: 基本类型(包括String)的值。
- set方法注入:
<property name="" ref=""> </property>
name: set方法名称,首字母要小写。
ref: 引用对象。
value: 基本类型(包括String)的值。
3. 注入的三种数据类型
- 基本数据类型
- 引用数据类型
- 集合数据类型
3.1 测试代码
- Student类
public class Student
{
// 基本数据类型
private int id;
private String username;
private String telephone;
// 引用数据类型
private StudentClass studentClass;
// List
private List<String> scores;
private List<StudentClass> list;
//Set
private Set<String> courses;
private Set<StudentClass> set;
//Map
private Map<String, StudentClass> map;
//Properties
private Properties properties;
public void setId(int id)
{
this.id = id;
}
public void setUsername(String username)
{
this.username = username;
}
public void setTelephone(String telephone)
{
this.telephone = telephone;
}
public void setStudentClass(StudentClass studentClass)
{
this.studentClass = studentClass;
}
public void setScores(List<String> scores)
{
this.scores = scores;
}
public void setList(List<StudentClass> list)
{
this.list = list;
}
public void setCourses(Set<String> courses)
{
this.courses = courses;
}
public void setSet(Set<StudentClass> set)
{
this.set = set;
}
public void setMap(Map<String, StudentClass> map)
{
this.map = map;
}
public void setProperties(Properties properties)
{
this.properties = properties;
}
@Override
public String toString()
{
return "Student{" +
"id=" + id +
", username='" + username + '\'' +
", telephone='" + telephone + '\'' +
", studentClass=" + studentClass +
", scores=" + scores +
", list=" + list +
", courses=" + courses +
", set=" + set +
", map=" + map +
", properties=" + properties +
'}';
}
}
- StudentClass类
public class StudentClass
{
private int cid;
private String cname;
public StudentClass()
{
}
public StudentClass(int cid, String cname)
{
this.cid = cid;
this.cname = cname;
}
public int getCid()
{
return cid;
}
public void setCid(int cid)
{
this.cid = cid;
}
public String getCname()
{
return cname;
}
public void setCname(String cname)
{
this.cname = cname;
}
@Override
public String toString()
{
return "StudentClass{" +
"cid=" + cid +
", cname='" + cname + '\'' +
'}';
}
}
- 测试代码
public class Main
{
@Test
public void test()
{
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) factory.getBean("cloud");
System.out.println(student);
}
}
- Spring配置文件
<?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">
<!-- 有参构造方法注入普通数据类型 -->
<bean name="spring" class="site.zhouyun.entity.StudentClass">
<property name="cid" value="10001"></property>
<property name="cname" value="Spring"></property>
</bean>
<!-- set方法注入普通数据类型 -->
<bean name="java" class="site.zhouyun.entity.StudentClass">
<property name="cid" value="10002"></property>
<property name="cname" value="Java"></property>
</bean>
<!-- set方法注入引用类型 -->
<bean id="student01" class="site.zhouyun.entity.Student">
<property name="studentClass" ref="java"></property>
</bean>
<bean name="cloud" class="site.zhouyun.entity.Student">
<property name="id" value="2020110601"></property>
<property name="username" value="孤云"></property>
<!-- null值注入 -->
<property name="telephone">
<null></null>
</property>
<property name="studentClass" ref="java"></property>
<!-- set方法注入List数据类型 -->
<property name="scores">
<!-- 有序、允许重复 -->
<list>
<value>100</value>
<value>90</value>
<value>87</value>
<value>78</value>
<value>90</value>
</list>
</property>
<property name="list">
<list>
<ref bean="spring"></ref>
<ref bean="java"></ref>
</list>
</property>
<!-- set方法注入Set数据类型 -->
<property name="courses">
<!-- 无序、不允许重复 -->
<set>
<value>Spring</value>
<value>Spring</value>
<value>Java</value>
<value>C</value>
<value>Python</value>
</set>
</property>
<property name="set">
<set>
<ref bean="java"></ref>
<ref bean="spring"></ref>
</set>
</property>
<!-- set方法注入Map数据类型 -->
<property name="map">
<map>
<entry key="001" value-ref="spring"></entry>
<entry key="002" value-ref="java"></entry>
</map>
</property>
<!-- set方法注入Properties数据类型 -->
<property name="properties">
<props>
<prop key="1">properties01</prop>
<prop key="2">properties02</prop>
<prop key="3">properties03</prop>
</props>
</property>
</bean>
</beans>
3.2 测试结果
Student{id=2020110601, username=‘孤云’, telephone=‘null’, studentClass=StudentClass{cid=10002, cname=‘Java’},
scores=[100, 90, 87, 78, 90], list=[StudentClass{cid=10001, cname=‘Spring’}, StudentClass{cid=10002, cname=‘Java’}], courses=[Spring, Java, C, Python], set=[StudentClass{cid=10002, cname=‘Java’}, StudentClass{cid=10001, cname=‘Spring’}], map={001=StudentClass{cid=10001, cname=‘Spring’}, 002=StudentClass{cid=10002, cname=‘Java’}}, properties={3=properties03, 2=properties02, 1=properties01}}