Spring IoC 学习(3)
来源:互联网 发布:sql with ur 编辑:程序博客网 时间:2024/05/29 18:00
点击查看全文
前言
前面因为总结的累了,把IoC的两个步骤,只写了一半,就仅仅把容器启动的方面说了说,对于实例化的阶段,我前面并没有说,在这节中,准备讲一讲,实例化阶段。
生命周期
基础生命周期简图
这个部分,其实实例化,一般都是用反射或者cglib,底层封装的也比较深,我随着代码debug的过程中,也没有接触到这个部分。但是在实例化bean的过程中,还是看到了挺多东西。
生命周期的图,基本上有可能是以下这种
从图中可以看到,在这个阶段,最重要的不是实例化本身,而是实例化前后会做的一些操作。实例化有些不同的,应该就是在实例化时可能会遇到绑定属性的相关操作,这个时候不是用传统的反射来做,而是用BeanWrapper来包装绑定。有个印象即可。
BeanFactory与ApplicationContext生命周期简图
BeanFactory
ApplicationContext
以上两图为借用
各种拓展接口
各色的Aware接口
当对象实例化完成并且相关属性以及依赖设置完成之后,Spring容器会检查当前对象实例是否实现了一系列的以Aware命名结尾的接口定义。如果是,则将这些Aware接口定义中规定的依赖注入给当前对象实例。
下面总结一下各种Aware接口以及作用
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);
点击查看全文
- Spring IoC 学习(3)
- Spring IoC 学习(3)
- spring学习3-IoC概述
- 技术文章 | Spring IoC 学习(3)
- spring IoC学习 ------IoC基本概念
- Spring学习笔记:3-IoC中的国际化
- Spring学习3:IOC容器概述
- Spring IoC学习笔记
- Spring学习-------IOC
- Spring Ioc学习(一)
- Spring Ioc学习(二)
- spring ioc学习
- Spring IOC 源码学习
- Spring学习-IOC容器
- Spring 学习之 IOC
- spring Ioc 的学习
- spring学习之Ioc
- Spring学习之IOC
- 运行npm run dev 时报错npm run install --save !!vue-style-loader!css-loader?
- ShaderGUI: Custom Material Inspectors in Unity 5+
- js判断变量是否未定义的代码
- 。net 后台将List数据转换为json后 前台js 调用
- 翻转字符串算法
- Spring IoC 学习(3)
- 第四章——文本输入和委托模式
- C++中的访问运算符(.和->)
- Mybatis-generator修改源代码实现自定义方法,返回List对象(三)
- 有用的pycharm快捷键收藏
- Tomcat 6 JNDI数据源详解
- FX3/CX3 yuv/rgb输出修改
- STM32-重定向C库printf 格式化输出符号详细说明
- 软件版本中的Alpha,Beta,RC,Trial是什么意思?