依赖注入(DI)
依赖注入简介
1、什么是依赖注入
- DI—Dependency Injection,即“依赖注入”是控制反转的一种实现方式。
- 由ioc容器在运行期间,动态的将某种依赖关系注入到对象之中也就是获取对象的过程由管理编程有ioc容器主动注入,通过反射机制实现
2、使用依赖注入的目的
- 谁依赖于谁:当然是应用程序依赖于IoC容器;
- 为什么需要依赖:应用程序需要IoC容器来提供对象需要的外部资源;
- 谁注入谁:很明显是IoC容器注入应用程序某个对象,应用程序依赖的对象;
- 注入了什么:就是注入某个对象所需要的外部资源(包括对象、资源、常量数据)
依赖注入的方式
1、构造器注入
- 使用的标签:
constructor-arg
- 属性标签中的属性
①type:
用于指定构造函数中的某个或者某些参数的数据类型
②index:
用于指定构造函数的位置,从0开始
③name:
用于指定构造函数中的参数的名字,更为常用
④value:
为基本数据类型和String类型注入具体的内容
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">
<bean id="p" class="com.jp.domain.Person">
<constructor-arg index="0" type="java.lang.String" value="张三"></constructor-arg>
<constructor-arg index="1" type="java.lang.Integer" value="18"></constructor-arg>
</bean>
</beans>
代码
public class Demo {
/*
* 方式一:
* 通过构造器传入值
*/
@Test
public void testConstructor(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Person p = (Person) ac.getBean("p");
System.out.println(p.name);
System.out.println(p.age);
}
}
2、Setter方法注入
- 使用的标签:property
- 不可以提供构造函数,因为构造函数注入和set方法注入是不兼容的
- 标签中的属性:
①name:
用于注入时所调用的方法名
②value:
为基本数据类型和String类型注入具体的内容
③ref:
用于指定其他的bean类型,它指定的是spring容器中出现过的bean对象
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">
<bean id="p" class="com.jp.domain.Person">
<property name="name" value="张三"></property>
<property name="age" value="18"></property>
<property name="s" ref="student"></property>
</bean>
<bean id="student" class="com.jp.domain.Student"></bean>
</beans>
代码
public class Demo {
/*
* 方式二
* 通过Set方法传入值
*/
@Test
public void testSetter(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Person p = (Person) ac.getBean("p");
System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getS());
}
}
3、集合属性的注入
List属性注入
- 用于给List集合注入的标签
:list
、array
、set
Set属性注入
- 用于给Set集合注入的标签:
set
Map属性注入
- 用于给map集合注入的标签:
map
、entry
Properties属性注入
- 用于给Properties集合注入的标签:
props
、prop
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">
<bean id="mc" class="com.jp.di.MyCollection">
<property name="list">
<list>
<value>123</value>
<value>456</value>
<ref bean="student"/>
<ref bean="p"/>
</list>
</property>
<property name="set">
<set>
<value>aaa</value>
<value>bbb</value>
<ref bean="student"></ref>
<ref bean="p"></ref>
</set>
</property>
<property name="map">
<map>
<entry key="aa" value="11"></entry>
<entry key="bb" value-ref="p"></entry>
<entry key-ref="p" value-ref="student"></entry>
</map>
</property>
<property name="prop">
<props>
<prop key="name">张三</prop>
<prop key="age">18</prop>
</props>
</property>
</bean>
</beans>
对象类代码
MyCollection类
public class MyCollection {
private List list;
private Set set;
private Map map;
private Properties prop;
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Properties getProp() {
return prop;
}
public void setProp(Properties prop) {
this.prop = prop;
}
}
Person类
public class Person {
private String name;
private Integer age;
private Student s;
public Person(){
System.out.println("Person对象已创建...");
}
public Person(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;
}
public void talk(){
System.out.println("讲话...");
}
public Student getS() {
return s;
}
public void setS(Student s) {
this.s = s;
}
}
Student类
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
测试代码
public class Demo {
/*
* 方式三:
* 通过集合传入值
* 1、List集合
*
* 2、Set集合
*
* 3、Map集合
*
* 4、Properties集合
*/
@Test
public void testList(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
MyCollection mc = (MyCollection) ac.getBean("mc");
System.out.println(mc.getList());
System.out.println(mc.getSet());
System.out.println(mc.getMap());
System.out.println(mc.getProp());
}
}