关于spring:Spring-学习笔记二SpringIoC

1 IoCDI

IoCInversion of Control的简称,也就是管制反转。通常来说,创建对象须要调用者手动创立,也就是new XXX()的形式。当Spring框架呈现后,对象的实例不再由调用者创立,而是由Spring容器创立,这样控制权就由调用者转移到Spring容器,控制权产生了反转,这就是Spring的管制反转。从Spring容器来看,Spring容器负责将被依赖对象赋值给调用者的成员变量,相当于为调用者注入它所依赖的实例,这就是Spring的依赖注入(Dependency InjectionDI)。

一句话总结:

  • IoC:控制权由调用者交由Spring容器,管制产生了反转
  • DI:由Spring容器注入须要的值到对象中

2 Spring IoC容器

Spring中实现IoC的是Spring IoC容器,次要基于以下两个接口:

  • BeanFactory
  • ApplicationContext

2.1 BeanFactory

位于org.springframework.beans.factory下,提供了残缺的IoC服务反对,是一个治理Bean工厂,次要负责初始化各种Bean。能够通过XmlBeanFactory来获取XML文件中的Bean并进行拆卸,例子如下:

BeanFactory factory = new XmlBeanFactory(new FileSystemResource("/xxx/xxx/xxx/xxx/applicationContext.xml"));
TestInterface test = (TestInterface)factory.getBean("test");
test.hello();

须要应用绝对路径,而且,该办法曾经过期了:

因而不举荐应用。

2.2 ApplicationContext

ApplicationContextBeanFactory的子接口,也称为利用上下文,除了蕴含BeanFactory的性能外还增加了国际化、资源拜访、事件流传等的反对,创立ApplicationContext的实例有以下三种办法:

  • ClassPathXmlApplicationContext
  • FileSystemXmlApplicationContext
  • Web服务器实例化

2.2.1 ClassPathXmlApplicationContext

该类从resources下寻找指定的XML文件:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
TestInterface test = (TestInterface)context.getBean("test");
test.hello();

2.2.2 FileSystemXmlApplicationContext

该类读取配置文件须要加上前缀:

  • classpath::该前缀示意从类门路读取,对于Maven我的项目来说就是resources
  • file::该前缀示意从绝对路径获取

例子:

ApplicationContext context = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
//ApplicationContext context = new FileSystemXmlApplicationContext("file:/xxx/xxx/xxx/xxxx/xxx/applicationContext.xml");

2.2.3 Web服务器实例化

个别应用基于ContextLoaderListener的实现形式,批改web.xml,增加如下代码:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
</context-param>

3 DI的两种办法

DI通常有两种实现形式:

  • 构造方法注入
  • setter注入

上面别离来看一下。

3.1 构造方法注入

Spring能够利用反射机制通过构造方法实现注入,比方有以下三个类:

public interface TestInterface {
    void hello();
}

public class TestA implements TestInterface {
    @Override
    public void hello() {
        System.out.println("Test A");
    }
}

public class TestB {
    private TestInterface test;

    public TestB(TestInterface test)
    {
        this.test = test;
    }

    public void method()
    {
        test.hello();
    }
}

TestInterface是一个简略的接口,而TestA实现了该接口,TestB须要一个TestInterface类型的对象,因而能够先注入一个TestA,再将该TestA注入到TestB的构造方法中:

<bean id="testA" class="TestA"/> <!--注入一个TestA对象-->
<bean id="testB" class="TestB">
    <constructor-arg index="0" ref="testA" /> <!--将下面注入的TestA作为参数传入构造方法中,在传给TestB的公有成员-->
</bean>

constructor-arg是用于定义通过构造方法的形式进行注入的标签,index定义地位,从0开始,ref是某个Bean的援用,值为该Beanid

3.2 通过setter注入

在下面的例子中,批改TestB如下:

public class TestB {
    private TestInterface test;

    public void setTest(TestInterface test) {
        this.test = test;
    }

    public void method()
    {
        test.hello();
    }
}

其实就是增加了一个setter,接着批改配置文件:

<bean id="testA" class="TestA"/>
<bean id="testB" class="TestB">
    <property name="test" ref="testA" />
</bean>

<property>示意通过setter注入,name是公有成员的名字,ref是被传入setterBeanid值。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理