Spring之IOC原理及代码详解

一、什么是IOC

  引用 Spring 官方原文:This chapter covers the Spring Framework implementation of the Inversion of Control (IoC) [1] principle. IoC is also known as dependency injection (DI). It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes, or a mechanism such as the Service Locator pattern.

  “控制反转(IoC)”也称为“依赖注入(DI)”,是一个定义对象依赖的过程,对象只和 构造参数,工厂方法参数,对象实例属性或工厂方法返回相关。容器在创建这些 bean 的时 候注入这些依赖。这个过程是一个反向的过程,所以命名为依赖反转,对象实例的创建由其 提供的构造方法或服务定位机制来实现。

  IOC 最大的好处就是“解耦”。

二、容器初始化流程

  spring为ApplicationContext提供了3种实现,分别是ClassPathXmlApplicationContext、FileSystemXmlApplicationContext、XmlWebApplicationContext,其中XmlWebApplicationContext是专为Web工程定制的。

  其中FileSystemXmlApplicationContext和ClassPathXmlApplicationContext与BeanFactory的xml文件定位方式一样是基于路径的。如:

AplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml"); //加载单个配置文件 

String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};  
ApplicationContext ctx new FileSystemXmlApplicationContext(locations ); //加载多个配置文件

ApplicationContext ctx =new FileSystemXmlApplicationContext("D:roject/bean.xml");//根据具体路径加载文件

ClassPathXmlApplicationContext同FileSystemXmlApplicationContext一样的初始化方式

  ClassPathXmlApplicationContext的类外部结构关系为:

  ContextLoaderListener / DispatcherServlet 创建的WebApplicationContext容器:

ServletContext servletContext = request.getSession().getServletContext();      
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);  

  XMLWebApplicationContext的类外部结构关系为:

  ApplicationContext 容器的初始化流程主要由 AbstractApplicationContext 类中的 refresh 方法实现。大致过程为:为 BeanFactory 对象执行后续处理(如:context:propertyPlaceholder等)->在上下文(Context)中注册 bean->为 bean 注册拦截处理器(AOP 相关)->初始化上 下文消息(初始化 id 为 messgeSource 的国际化 bean 对象)->初始化事件多播(处理事件监 听,如ApplicationEvent等)->初始化主题资源(SpringUI 主题 ThemeSource)->注册自定义 监听器->实例化所有非 lazy-init 的 singleton 实例->发布相应事件(Lifecycle 接口相关实现类的生命周期事件发布)。

  在 spring 中,构建容器的过程都是同步的。同步操作是为了保证容器构建的过程中,不出现多线程资源冲突问题(因为对象的构建、资源的扫描、文件的扫描如果存在多线程对文件的扫描问题,会出现锁的问题)。

public void refresh() throws BeansException, IllegalStateException {
    Object var1 = this.startupShutdownMonitor;
    synchronized(this.startupShutdownMonitor) {
        // Prepare this context for refreshing.
        this.prepareRefresh();
        // Tell the subclass to refresh the internal bean factory.
        ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
        // Prepare the bean factory for use in this context.
        this.prepareBeanFactory(beanFactory);

        try {
            // Allows post-processing of the bean factory int context subclasses.
            this.postProcessBeanFactory(beanFactory);
            // Invoke factory processors registered as beans int the context.
            this.invokeBeanFactoryPostProcessors(beanFactory);
            // Register bean processors that intercept bean creation.
            this.registerBeanPostProcessors(beanFactory);
            // Initialize message source for this context.
            this.initMessageSource();
            // Initialize event muticaster for this context.
            this.initApplicationEventMulticaster();
            // Initialize other special beans in specific context subclasses.
            this.onRefresh();
            // Check for listener beans and register them.
            this.registerListeners();
            // Instantiate all remaining(non-lazy-init) singletons.
            this.finishBeanFactoryInitialization(beanFactory);
            // Last step: publish corresponding event.
            this.finishRefresh();
        } catch (BeansException var9) {
            if (this.logger.isWarnEnabled()) {
                this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
            }

            this.destroyBeans();
            this.cancelRefresh(var9);
            throw var9;
        } finally {
            this.resetCommonCaches();
        }

    }
}
  • prepareRefresh - 准备上下文环境信息,为接下来ApplicationContext 容器的初始化流程做准备。

  • BeanFactory 的构建。 BeanFactory 是 ApplicationContext 的父接口。是 spring 框架中的顶级容器工厂对象。BeanFactory 只能管理 bean 对象。没有其他功能。如:aop 切面管理、propertyplaceholder 的加载等就不属于BeanFactory的管理范围。 构建 BeanFactory 的功能就是管理 bean 对象。创建 BeanFactory 中管理的 bean 对象。
  • postProcessBeanFactory - 加载配置中 BeanFactory 无法处理的内容 。如:propertyplacehodler 的加载。
  • invokeBeanFactoryPostProcessors- 将上一步加载的内容,作为一个容器可以管理的bean对象注册到 ApplicationContext 中。底层实质是在将 postProcessBeanFactory 中加载的内容包装成一个容器 ApplicationContext 可以管理的 bean 对象。
  • registerBeanPostProcessors - 继续完成上一步的注册操作。配置文件中配置的 bean 对象都创建并注册完成。
  • initMessageSource - i18n,国际化。初始化国际化消息源。
  • initApplicationEventMulticaster - 注册事件多播监听。如 ApplicationEvent 事件。是 spring 框架中的观察者模式实现机制。
  • onRefresh - 初始化主题资源(ThemeSource)。spring 框架提供的视图主题信息。 registerListeners- 创建监听器,并注册。
  • finishBeanFactoryInitialization - 初始化配置中出现的所有的 lazy-init=false 的 bean 对象,且 bean 对象必须是 singleton 的。
  • finishRefresh - 最后一步。 发布最终事件。生命周期监听事件。 spring 容器定义了生命周期接口。可以实现容器启动调用初始化,容器销毁之前调用回收资源。Lifecycle 接口。

三、多容器/父子容器概念

  Spring 框架允许在一个应用中创建多个上下文容器。但是建议容器之间有父子关系。可以通过 ConfigurableApplicationContext 接口中定义的 setParent 方法设置父容器。一旦设置父 子关系,则可以通过子容器获取父容器中除 PropertyPlaceHolder 以外的所有资源,父容器不能获取子容器中的任意资源(类似 Java 中的类型继承)。

  典型的父子容器: spring 和 springmvc 同时使用的时候。ContextLoaderListener 创建的容器是父容器,DispatcherServlet 创建的容器是子容器。

  父子容器的存在是保证一个 JVM 中,只有一个树状结构的容器树,每个容器都有一定的作用范围和访问域,起到一个隔离的作用。可以通过子容器访问父容器资源。

四、p 域/c 域

  Spring2.0 之后引入了 p(property 标签)域、Spring3.1 之后引入了 c(constractor-arg 标签) 域。可以简化配置文件中对 property 和 constructor-arg 的配置。

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.spring framework.org/schema /c" xmlns:p="http://www.sprin gframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org /schema/beans
http://www.springframework.org /schema /beans/spri ng-bean s.xsd">
  <bean id="oneBean" class="com.sxt.OneBean" p:a="10" p:o-ref="otherBean" c:a="20" c:o-ref="otherBean"/>
  <bean id="otherBean" class="com.sxt.OtherBean" />
</beans>
class OneBean{ 
    int a;
    Object o;
    public OneBean(int a, Object o){ 
        this.a = a; 
        this.o = o;
    } // getters and setters for fields.
}

五、lookup-method

  lookup-method 一旦应用,Spring 框架会自动使用 CGLIB 技术为指定类型创建一个动态子类型,并自动实现抽象方法。可以动态的实现依赖注入的数据准备。

  在效率上,比直接自定义子类型慢。相对来说更加通用。可以只提供 lookup-method 方法的返回值对象即可实现动态的对象返回。

  在工厂方法难以定制的时候使用,也是模板的一种应用,工厂方法的扩展。如:工厂方法返回对象类型为接口类型,且不同版本应用返回的对象未必相同时使用,可以避免多次开发工厂类。如:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestLookupMethod {
    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:lookupmethod/applicationContext.xml");

        CommandManager manager = context.getBean("manager", CommandManager.class);

        System.out.println(manager.getClass().getName());
        manager.process();
    }
}

abstract class CommandManager {
    public void process() {
        MyCommand command = createCommand();
        // do something ...
        System.out.println(command);
    }

    protected abstract MyCommand createCommand();
}

interface MyCommand {
}

class MyCommand1 implements MyCommand {
    public MyCommand1() {
        System.out.println("MyCommand1 instanced");
    }
}

class MyCommand2 implements MyCommand {
    public MyCommand2() {
        System.out.println("MyCommand2 instanced");
    }
}

  applicationContext.xml中配置为:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="manager" class="com.sxt.lookupmethod.CommandManager">
        <lookup-method bean="command2" name="createCommand"/>
    </bean>

    <bean id="command1" class="com.sxt.lookupmethod.MyCommand1"></bean>
    <bean id="command2" class="com.sxt.lookupmethod.MyCommand2"></bean>

</beans>

猜你喜欢

转载自www.cnblogs.com/jing99/p/11809770.html
今日推荐