点击查看全文
前面因为总结的累了,把IoC的两个步骤,只写了一半,就仅仅把容器启动的方面说了说,对于实例化的阶段,我前面并没有说,在这节中,准备讲一讲,实例化阶段。
这个部分,其实实例化,一般都是用反射或者cglib,底层封装的也比较深,我随着代码debug的过程中,也没有接触到这个部分。但是在实例化bean的过程中,还是看到了挺多东西。
生命周期的图,基本上有可能是以下这种
从图中可以看到,在这个阶段,最重要的不是实例化本身,而是实例化前后会做的一些操作。实例化有些不同的,应该就是在实例化时可能会遇到绑定属性的相关操作,这个时候不是用传统的反射来做,而是用BeanWrapper来包装绑定。有个印象即可。
BeanFactory ApplicationContext
以上两图为借用
当对象实例化完成并且相关属性以及依赖设置完成之后,Spring容器会检查当前对象实例是否实现了一系列的以Aware命名结尾的接口定义。如果是,则将这些Aware接口定义中规定的依赖注入给当前对象实例。
下面总结一下各种Aware接口以及作用
LoadTimeWeaverAware 加载Spring Bean时织入第三方模块,如AspectJ BeanClassLoaderAware 加载Spring Bean的类加载器 ResourceLoaderAware 底层访问资源的加载器 BeanFactoryAware 得到BeanFactory引用 ServletConfigAware 得到ServletConfig ServletContextAware 得到ServletContext MessageSourceAware 国际化 ApplicationEventPublisherAware 应用事件我们看一下这个接口
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; }① 在注解时使用
在使用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);
点击查看全文
