Spring IoC
文章目录
前言
IoC(控制反转)是Spring框架的基础,也是Spring框架的核心理念,下面将介绍IoC的基本概念,容器以及依赖注入的类型等内容和代码演示
一、SpringIoC是什么?
SpringIoC我自己理解为Spring容器帮我们创建Java对象,用于减少调用者与被调用者之间的耦合性。Spring容器会负责控制程序之间的关系(例如面包负责控制您与面包的关系)。而不是由调用者的程序代码直接控制。这样,控制权由调用者转移到Spring容器,控制发生了反转,这就是Spring的控制反转。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入
二、SpringIoC容器
实现控制反转的就是SpringIoC容器,
1.BeanFactory
这里不做详细介绍,因为使用BeanFactory实例加载Sprring配置文件在实际开发中并不多见,了解即可。
常用XmlBeanFactory装配Bean,在创建BeanFactory实例时需要提供XML文件的绝对路径
public static void main(String [] args){
BeanFactory beanFac = new XmlBeanFactory(new FileSystemResource(D
:\eclipse-workspace\test\src\applicationContext.xml")
):
TestDao tt = (TestDao)beanFac,getBean("test");
test.sayHello():
}
2.ApplicationContext
ApplicationContext是BeanFactory的子接口,也称为应用上下文,除了包含BeanFactory所有功能外,还添加对国际化,资源访问,事件传播等内容的支持。
创建实例通常有三种方法
1.通过ClassPathXmlApplicationContext创建(从类路径目录(src根目录))
public static void main(String [] args){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
TestDIService ts = (TestDIService) applicationContext.getBean("testDIservice");
ts.sayhello();
}
2.通过FileSystemXmlApplicationContext创建(绝对路径)
public static void main(String [] args){
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("D:\workspace\test\src\applicationContext.xml");
TestDIService ts = (TestDIService) applicationContext.getBean("testDIservice");
ts.sayhello();
}
3.在Web应用中,ApplicationContext容器的实例化工作将交给Web服务器完成。一般使用基于ContextLoaderListener的实现方法(需要导入spring-web-5.0.2.RELEASE.jar)并在web.xml中添加如下代码
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
三、依赖注入的类型
实现IoC容器的方法就是依赖注入
1.使用构造方法
即通过构造方法在创建Bean对象是动态的将其所依赖的对象(属性值之类的)注入Bean组件中
下面通过实例演示
public interface TestDIDao {
public void sayHello();
}
public class TestDIDaoimpl implements TestDIDao{
@Override
public void sayHello() {
System.out.println("hello!");
}
}
public interface TestDIService {
public void sayhello();
}
public class TestDIServiceimpl implements TestDIService {
private TestDIDao testDIDao;
public TestDIServiceimpl(TestDIDao testDIDao) {
this.testDIDao = testDIDao;
}
@Override
public void sayhello() {
testDIDao.sayHello();
System.out.println("TestDIservice构造方法注入say:hello!");
}
}
public class TestDI {
public static void main(String [] args){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
TestDIService ts = (TestDIService) applicationContext.getBean("testDIservice");
ts.sayhello();
}
}
<?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="myTestDiDao" class="dao.TestDIDaoimpl"/>
<bean id="testDIservice" class="service.TestDIServiceimpl">
<constructor-arg index="0" ref="myTestDiDao"/>
</bean>
</beans>
运行结果
2.使用setter方法注入
即通过setter方法注入Bean中的属性值,在上面那个例子中将serviceimpl类中的TestDao对象添加一个setter方法,修改配置文件中的Bean为
<bean id="testDIservice" class="service.TestDIServiceimpl">
<property name="testDIdao" ref="myTestDiDao"/>
</bean>
这就是setter方法注入的简单模拟