spring实现ioc容器的方法是依赖注入,依赖注入的作用是在使用Spring框架创建对象时动态的将其所依赖的对象(例如属性值)注入Bean组件中。
spring框架的依赖注入通常有两种实现方式,一种是使用构造方法注入,另一种是使用属性的setter方法注入。
使用构造方法注入
项目结构如下
1.创建dao包,并创建dao层的接口和实现类
接口TestDIDao的代码如下
package dao;
public interface TestDIDao {
public void sayHello();
}
实现类TestDIDaoImpl的代码如下
package dao;
public class TestDIDaoImpl implements TestDIDao {
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("TestDIDao say:fdssb1313gsb4");
}
}
2.创建service包,并创建service层的接口和实现类
接口TestDIService的代码如下
package service;
public interface TestDIService {
public void sayHello();
}
实现类TestDIServiceImpl的代码如下
package service;
import dao.TestDIDao;
public class TestDIServiceImpl implements TestDIService {
private TestDIDao tdo;
//构造方法,用于实现依赖注入接口对象tdo
public TestDIServiceImpl(TestDIDao tdo) {
super();
this.tdo = tdo;
}
@Override
public void sayHello() {
// TODO Auto-generated method stub
tdo.sayHello();
System.out.println("TestDIService构造方法注入sayhello");
}
}
3.编写配置文件ApplicationContext.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">
<!-- 将 dao.TestDIDaoImpl托管给spring -->
<bean id="mytestDao" class="dao.TestDIDaoImpl"></bean>
<!-- 使用构造方法注入 -->
<bean id="testDIService" class="service.TestDIServiceImpl">
<!-- 将 mytestDao注入到TestDIServiceImpl类的属性TestDIDao上 -->
<constructor-arg index="0" ref="mytestDao" />
</bean>
<!--
constructor-arg 定义类构造方法的参数
index 定义参数的位置
ref 指定某个实例的引用,如果参数是常量值,ref由value代替
-->
</beans>
4.创建测试包,并创建测试类
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.TestDIService;
public class Test {
public static void main(String[] args) {
//初始化Spring容器ApplicationContext,加载配置文件
ApplicationContext appCon =new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器获取test实例
TestDIService ts =(TestDIService)appCon.getBean("testDIService");
ts.sayHello();
}
}
5.运行结果
使用属性的setter方法注入
使用setter方法注入是Spring框架中最主流的注入方式,对于setter方法注入,Spring框架也是使用java的反射机制实现的。
1.创建接口实现类TestDIServiceImpl2,并使用setter方法注入TestDIDao接口对象
package service;
import dao.TestDIDao;
public class TestDIServiceImpl2 implements TestDIService{
private TestDIDao tdo;
//使用setter方法注入TestDIDao接口对象
public void setTdo(TestDIDao tdo) {
this.tdo = tdo;
}
@Override
public void sayHello() {
tdo.sayHello();
System.out.println("testDIService2 --setter方法注入");
}
}
2.将TestDIServiceImpl2托管给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">
<!-- 将 dao.TestDIDaoImpl托管给spring -->
<bean id="mytestDao" class="dao.TestDIDaoImpl"></bean>
<!-- 使用构造方法注入 -->
<bean id="testDIService" class="service.TestDIServiceImpl">
<!-- 将 mytestDao注入到TestDIServiceImpl类的属性TestDIDao上 -->
<constructor-arg index="0" ref="mytestDao" />
</bean>
<!--
constructor-arg 定义类构造方法的参数
index 定义参数的位置
ref 指定某个实例的引用,如果参数是常量值,ref由value代替
-->
<!-- 使用setter方法注入 -->
<bean id="testDIService2" class="service.TestDIServiceImpl2">
<property name="tdo" ref="mytestDao"></property>
<!--
name setter方法注入的属性
ref 指定某个实例的引用
-->
</bean>
</beans>
3.测试代码
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.TestDIService;
public class Test {
public static void main(String[] args) {
//初始化Spring容器ApplicationContext,加载配置文件
ApplicationContext appCon =new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器获取test实例
TestDIService ts =(TestDIService)appCon.getBean("testDIService");
ts.sayHello();
TestDIService ts2 =(TestDIService)appCon.getBean("testDIService2");
ts2.sayHello();
}
}