Spring依赖注入 DI
Spring依赖注入 DI
DI:Dependency Injection(依赖注入)
从字面上分析:
IoC:指将对象的创建权,反转给了Spring容器;
DI :指Spring创建对象的过程中,将对象依赖属性(简单值,集合,对象)通过配置设值给该对象。
IoC和DI其实是同一个概念的不同角度描述,DI相对IoC而言,明确描述了“被注入对象依赖IoC容器配置依赖对象”。
所谓的依赖注入,就是属性不创建对象,通过配置文件的配置将Spring容器里面的对象注入给对应的属性
依赖注入有四种方式
setter注入(属性注入)
1.setter注入,(也可以称之为属性注入)
使用setter注入:
1,使用bean元素的子元素设置;
1,简单类型值,直接使用value赋值;
2,引用类型,使用ref赋值;
3,集合类型,直接使用对应的集合类型元素即可。
2,spring通过属性的setter方法注入值;
3,在配置文件中配置的值都是string,spring可以自动的完成类型的转换
package cn.xc.spring.bean;
public class Employee {
private Integer age;
private String name;
private Department dept;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Employee [age=" + age + ", name=" + name + ", dept=" + dept + "]";
}
public Employee() {
// TODO Auto-generated constructor stub
}
public Employee(Integer age, String name, Department dept) {
super();
this.age = age;
this.name = name;
this.dept = dept;
}
}
package cn.xc.spring.bean;
public class Department {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department() {
// TODO Auto-generated constructor stub
}
public Department(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Department [id=" + id + ", name=" + name + "]";
}
}
测试类
package cn.xc.spring.test;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.xc.spring.bean.Employee;
public class SpringTest {
@Test
public void testName() throws Exception {
// 1.读取Spring配置文件,启动Spring框架,创建Spring容器对象
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Employee emp = context.getBean("emp", Employee.class);
System.out.println(emp);
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring配置的根元素+约束 -->
<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">
<!-- IOC -->
<!-- 部门 -->
<bean id="department" class="cn.xc.spring.bean.Department">
<!-- DI:依赖注入
set方法(属性)注入
<property name="" ></property>
name :要注入的属性名称
value:值类型注入(字符串+基本数据类型)
ref : 对象/引用类型注入
-->
<property name="id" value="123"/>
<property name="name" value="开发部"/>
</bean>
<bean id="emp" class="cn.xc.spring.bean.Employee">
<!-- DI:依赖注入
set方法(属性)注入
<property name="" ></property>
name :要注入的属性名称
value:值类型注入(字符串+基本数据类型)
ref : 对象/引用类型注入
-->
<property name="name" value="杰克"/>
<property name="age" value="25"/>
<property name="dept" ref="department"/>
</bean>
</beans>
构造器注入
<!--
1,默认情况下,constructor-arg的顺序就是构造器参数的顺序
2,3中调整构造器顺序:
1.index:在构造器中的参数索引(从0开始)
2.type:在构造器中的参数的类型
3.name:在构造器中按照构造器的参数名字设置值
====================
使用哪种注入方式比较好(setter?构造器?)?
1,如果一个类必须依赖另一个类才能正常运行,用构造器;
2,但是构造器的参数如果过多,构造器很难看;
3,更多的还是使用setter注入;
4,可以使用@Required标签来要求一个属性必须注入
-->
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring配置的根元素+约束 -->
<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">
<!-- IOC -->
<!-- 部门 -->
<bean id="department" class="cn.xc.spring.bean.Department">
<!-- DI :构造器注入 constructor(构造函数)
<constructor-arg index="" name="" type="" value="" ref=""/>
index : 参数的位置
name :参数名称
type :参数类型
value: 值类型
ref : 引用/对象类型
-->
<constructor-arg name="id" value="123123"/>
<constructor-arg name="name" value="销售部"/>
</bean>
<bean id="emp" class="cn.xc.spring.bean.Employee">
<constructor-arg name="name" value="lucy"/>
<constructor-arg name="age" value="18"/>
<constructor-arg name="dept" ref="department"/>
</bean>
</beans>
p命名空间注入
使用p命名空间注入先在约束上面引入 p标签
或者
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring配置的根元素+约束 -->
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- IOC -->
<!-- 部门 -->
<bean id="department" class="cn.xc.spring.bean.Department"
p:id="2123154"
p:name="技术部"
/>
<bean id="emp" class="cn.xc.spring.bean.Employee"
p:name="lili"
p:age="18"
p:dept-ref="department"
/>
</beans>
集合类型值注入
在处理的数据中,
有标量类型=基础数据类型以及包装类+String – value属性
也有Spring容器里面的对象 --ref属性
还要很多数据JDK内置的数据结构:
- 键值对 Map 、Properties
- 数组
- 集合Set、List
package cn.xc.spring.pojo;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class CollectionBean {
private String[] arr;
private List<Object> list;
private Set<String> set;
private Map<String, String> map;
private Properties prop; // 读取 xxx.properties 配置文件内容
public String[] getArr() {
return arr;
}
public void setArr(String[] arr) {
this.arr = arr;
}
public List<Object> getList() {
return list;
}
public void setList(List<Object> list) {
this.list = list;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Properties getProp() {
return prop;
}
public void setProp(Properties prop) {
this.prop = prop;
}
public CollectionBean() {
// TODO Auto-generated constructor stub
}
public CollectionBean(String[] arr, List<Object> list, Set<String> set, Map<String, String> map, Properties prop) {
super();
this.arr = arr;
this.list = list;
this.set = set;
this.map = map;
this.prop = prop;
}
@Override
public String toString() {
return "CollectionBean [arr=" + Arrays.toString(arr) + ", list=" + list + ", set=" + set + ", map=" + map
+ ", prop=" + prop + "]";
}
}
配置文件
<?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 id="collectionBean" class="cn.xc.spring.pojo.CollectionBean">
<!--数组 -->
<property name="arr">
<array>
<value>luxi</value>
<value>lili</value>
<value>jum</value>
</array>
</property>
<!-- list集合 -->
<property name="list">
<list>
<value>qf</value>
<value>xuzhu</value>
<value>duanyu</value>
<ref bean="userMapper1" />
<ref bean="userMapper2" />
</list>
</property>
<!-- set -->
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<!-- map -->
<property name="map">
<map>
<entry key="key1" value="v11"></entry>
<entry key="key1" value="v12"></entry>
<entry key="key2" value="v11"></entry>
</map>
</property>
<!-- properties -->
<property name="prop">
<props>
<prop key="p1">p1</prop>
<prop key="p2">p2</prop>
<prop key="p3">p3</prop>
</props>
</property>
</bean>
<bean id="userMapper1" class="cn.xc.spring.mapper.UserMapper"></bean>
<bean id="userMapper2" class="cn.xc.spring.mapper.UserMapper"></bean>
</beans>
测试代码
package cn.xc.spring.test;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.xc.spring.pojo.CollectionBean;
public class SpringTest {
@Test
public void testName() throws Exception {
// 1.读取Spring配置文件,启动Spring框架,创建Spring容器对象
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
CollectionBean bean = context.getBean("collectionBean", CollectionBean.class);
System.out.println(bean);
}
}