依赖注入类型

spring通过java的反射机制,动态的把依赖的对象注入bean中 ;DI有两种方法:基于构造函数的DI和基于 setter 方法 的DI

反射是程序可以访问、检测和修改它本身状态或行为的一种能力 ;通俗的说就是,需要动态的加载一些类这些类可能之前用不到所以不用加载到jvm,而是在运行时根据需要才加载

示例

步骤

1.导入依赖包

2.创建接口类TestDI,TestDIServer

3.创建实现类TestDIImpl,TestDIServerImpl1,TestDIServerImpl2

4.测试类test

5.配置bean文件 applicationContext.xml

代码

TestDI接口

1
2
3
4
package TestDI;
public interface TestDI {
public void testDI();
}

TestDIImpl实现类

1
2
3
4
5
6
package TestDI;
public class TestDIImpl implements TestDI{
public void testDI() {
System.out.println("正常的调用");
}
}

TestDIServer接口

1
2
3
4
package TestDIServer;
public interface TestDIServer {
public void testDIServer();
}

TestDIServerImpl1实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package TestDIServer;

import TestDI.TestDI;

public class TestDIServerImpl1 implements TestDIServer {
private TestDI testDI;

// 构造方法
public TestDIServerImpl1(TestDI testDI) {
super();
this.testDI = testDI;
}

public void testDIServer() {
// 调用testDI的testDI()方法
testDI.testDI();
System.out.println("构造方法注入");
}
}

TestDIServerImpl2实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package TestDIServer;

import TestDI.TestDI;

public class TestDIServerImpl2 implements TestDIServer {
private TestDI testDI;

// 添加testDI属性的setter方法
public void setTestDI(TestDI testDI) {
this.testDI = testDI;
}

public void testDIServer() {
// 调用testDI的testDI()方法
testDI.testDI();
System.out.println("setter方法注入");
}
}

test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package test;
import org.springframework.context.ApplicationContext;
import TestDIServer.TestDIServer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) {
//初始化Spring容器ApplicationContext,加载配置文件
ApplicationContext appCon = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器获取TestDIServerImpl1实例,测试构造方法 注入
TestDIServer ts1 = (TestDIServer)appCon.getBean("TestDIServerImpl1");
ts1.testDIServer();
//通过容器获取TestDIServerImpl2实例,测试setter方法注入
TestDIServer ts2 = (TestDIServer)appCon.getBean("TestDIServerImpl2");
ts2.testDIServer();
}
}

applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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">

<!-- 将指定类TestDIImpl配置给Spring,让Spring创建其实例 -->
<bean id="TestDI" class="TestDI.TestDIImpl" />

<!-- 使用构造方法注入 -->
<bean id="TestDIServerImpl1" class="TestDIServer.TestDIServerImpl1">
<!-- 将TestDI注入到TestDIServerImpl1类的属性 testDI上-->
<constructor-arg index="0" ref="TestDI"/>
</bean>

<!-- 使用setter方法注入 -->
<bean id="TestDIServerImpl2" class="TestDIServer.TestDIServerImpl2">
<!-- 调用TestDIServiceImpl2类的setter方法,将TestDI注入到 TestDIServiceImpl2类的属性testDI上-->
<property name="testDI" ref="TestDI"/>
</bean>

</beans>

结果

1
2
3
4
正常的调用
构造方法注入
正常的调用
setter方法注入