Spring IoC 学习(3)

来源:互联网 发布:sql with ur 编辑:程序博客网 时间:2024/05/29 18:00

点击查看全文


前言

前面因为总结的累了,把IoC的两个步骤,只写了一半,就仅仅把容器启动的方面说了说,对于实例化的阶段,我前面并没有说,在这节中,准备讲一讲,实例化阶段。

生命周期

基础生命周期简图

这个部分,其实实例化,一般都是用反射或者cglib,底层封装的也比较深,我随着代码debug的过程中,也没有接触到这个部分。但是在实例化bean的过程中,还是看到了挺多东西。

生命周期的图,基本上有可能是以下这种
image.png | center | 801x332

从图中可以看到,在这个阶段,最重要的不是实例化本身,而是实例化前后会做的一些操作。实例化有些不同的,应该就是在实例化时可能会遇到绑定属性的相关操作,这个时候不是用传统的反射来做,而是用BeanWrapper来包装绑定。有个印象即可。

BeanFactory与ApplicationContext生命周期简图

BeanFactory
image.png | center | 585x501
ApplicationContext
image.png | center | 585x501

以上两图为借用

各种拓展接口

各色的Aware接口

当对象实例化完成并且相关属性以及依赖设置完成之后,Spring容器会检查当前对象实例是否实现了一系列的以Aware命名结尾的接口定义。如果是,则将这些Aware接口定义中规定的依赖注入给当前对象实例。

下面总结一下各种Aware接口以及作用

LoadTimeWeaverAware加载Spring Bean时织入第三方模块,如AspectJBeanClassLoaderAware加载Spring Bean的类加载器ResourceLoaderAware底层访问资源的加载器BeanFactoryAware得到BeanFactory引用ServletConfigAware得到ServletConfigServletContextAware得到ServletContextMessageSourceAware国际化ApplicationEventPublisherAware应用事件

BeanPostProcessor

我们看一下这个接口

package org.springframework.beans.factory.config;import org.springframework.beans.BeansException;/** *  */public interface BeanPostProcessor {    /**     *  初始化之前做操作     */    Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;    /**     *  初始化之后做操作     */    Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;}

BeanPostProcessor的应用场景

① 在注解时使用

在使用Spring构建目的时候,现在应该很多人都习惯于用注解了,因为注解简单。@Component @Controller @Service @Repository @Autowired 等注解来便捷开发,下面来探讨BeanPostProcessor在@Autowired 中的运用。

在使用@Autowired之前需要在容器中配置AutowiredAnnotationBeanPostProcessor。

② 处理Aware接口类

我们可以来看一小段ApplicationContextAwareProcessor的代码

package org.springframework.context.support;/** * 可以看到是实现自BeanPostProcessor的 */class ApplicationContextAwareProcessor implements BeanPostProcessor {    /**     * 略去部分代码     */    /**     * 在初始化之前做的操作     */    @Override    public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {        AccessControlContext acc = null;        if (System.getSecurityManager() != null &&                (bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||                        bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||                        bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) {            acc = this.applicationContext.getBeanFactory().getAccessControlContext();        }        if (acc != null) {            AccessController.doPrivileged(new PrivilegedAction<Object>() {                @Override                public Object run() {                    invokeAwareInterfaces(bean);                    return null;                }            }, acc);        }        else {            // 直奔重点,invoke这些Aware接口            invokeAwareInterfaces(bean);        }        return bean;    }    private void invokeAwareInterfaces(Object bean) {        if (bean instanceof Aware) {            if (bean instanceof EnvironmentAware) {                ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());            }            if (bean instanceof EmbeddedValueResolverAware) {                ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(                        new EmbeddedValueResolver(this.applicationContext.getBeanFactory()));            }            if (bean instanceof ResourceLoaderAware) {                ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);            }            if (bean instanceof ApplicationEventPublisherAware) {                ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);            }            if (bean instanceof MessageSourceAware) {                ((MessageSourceAware) bean).setMessageSource(this.applicationContext);


点击查看全文


原创粉丝点击