spring(三):spring中BeanPostProcessor的使用
spring中实现BeanPostProcessor的后置处理器

ApplicationContextAwareProcessor
进入该实现类内部

可以看到:该类帮我们组建IOC容器,判断我们的bean有没有实现ApplicationContextAware接口,并作出相应处理(setApplicationContext方法)
测试:调试一个实现ApplicationContextAware接口的类的创建过程
public class User implements ApplicationContextAware {
private ApplicationContext applicationContext;
public User(){
System.out.println("User...constructor....");
}
@PostConstruct
public void init(){
System.out.println("User...init....");
}
@PreDestroy
public void destroy(){
System.out.println("User...destroy....");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
/**
* 容器初始化,将applicationContext作为参数传入,其它方法就可以使用容器了
* 该方法是由ApplicationContextAwareProcessor处理器来执行的
*/
this.applicationContext = applicationContext;
}
}
在ApplicationContextAwareProcessor类的postProcessBeforeInitialization方法中添加断点
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)) {//1
acc = this.applicationContext.getBeanFactory().getAccessControlContext();
}
if (acc != null) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
ApplicationContextAwareProcessor.this.invokeAwareInterfaces(bean);
return null;
}
}, acc);
} else {
this.invokeAwareInterfaces(bean);//2
}
return bean;
}
注释1:创建User对象,初始化之前,先判断User类是否实现了ApplicationContextAware接口
注释2:调用invokeAwareInterfaces方法给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(this.embeddedValueResolver);
}
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);
}
if (bean instanceof ApplicationContextAware) {
((ApplicationContextAware)bean).setApplicationContext(this.applicationContext);
}
}
}
invokeAwareInterfaces方法中判断该bean属于哪个Aware实例,并进行相应设值处理。如果Aware是ApplicationContextAware,就将当前bean转成ApplicationContextAware类型并调用setApplicationContext方法将ApplicationContext值设置到bean中
调试:
在bean是User类型时,setApplicationContext方法会得到执行,也即user对象中设置了ioc容器,可以在user的其它方法中使用该容器
BeanValidationPostProcessor
数据校验
常用
对象创建完成,bean对象属性赋值后,web中应用比较多
对页面提交的值进行校验
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (!this.afterInitialization) {
this.doValidate(bean);
}
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (this.afterInitialization) {
this.doValidate(bean);
}
return bean;
}
postProcessBeforeInitialization方法用于初始化前校验
postProcessAfterInitialization方法用于初始化后校验
InitDestroyAnnotationBeanPostProcessor
该处理器用于处理@PostConstruct、@PreDestroy注解
利用反射调用bean生命周期的相关方法

spring(三):spring中BeanPostProcessor的使用的更多相关文章
- spring(三) spring事务操作
前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...
- Spring(三) Spring IOC
Spring 核心之 IOC 容器 再谈 IOC 与 DI IOC(Inversion of Control)控制反转:所谓控制反转,就是把原先我们代码里面需要实现的对象创 建.依赖的代码,反转给容器 ...
- Spring(三) Spring IOC 初体验
Web IOC 容器初体验 我们还是从大家最熟悉的 DispatcherServlet 开始,我们最先想到的还是 DispatcherServlet 的 init() 方法.我们发现在 Dispath ...
- spring中BeanPostProcessor之一:InstantiationAwareBeanPostProcessor(01)
在spring中beanPostProcessor绝对是开天辟地的产物,给了程序员很多自主权,beanPostProcessor即常说的bean后置处理器. 一.概览 先来说下Instantiatio ...
- spring中BeanPostProcessor之三:InitDestroyAnnotationBeanPostProcessor(01)
在<spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)>一文中,分析到在调用CommonAnnotationB ...
- spring中BeanPostProcessor之四:AutowiredAnnotationBeanPostProcessor(01)
在<spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)>中分析了CommonAnnotationBeanPos ...
- Spring中BeanPostProcessor
Spring中BeanPostProcessor 前言: 本文旨在介绍Spring动态配置数据源的方式,即对一个DataSource的配置诸如jdbcUrl,user,password,driverC ...
- spring(三、spring中的eheche缓存、redis使用)
spring(三.spring中的eheche缓存.redis使用) 本文主要介绍为什么要构建ehcache+redis两级缓存?以及在实战中如何实现?思考如何配置缓存策略更合适?这样的方案可能遗留什 ...
- Spring Boot2 系列教程(三)理解 Spring Boot 项目中的 parent
前面和大伙聊了 Spring Boot 项目的三种创建方式,这三种创建方式,无论是哪一种,创建成功后,pom.xml 坐标文件中都有如下一段引用: <parent> <groupId ...
- Spring(三)Bean继续入门
一.Aware相关接口 对于应用程序来说,应该尽量减少对Sping Api的耦合程度,然而有些时候为了运用Spring所提供的一些功能,有必要让Bean了解Spring容器对其进行管理的细节信息,如让 ...
随机推荐
- vue 概念与使用vue-cli脚手架快速构建项目
vue 定义:是一套构建用户界面的渐进式框架,Vue 采用自底向上增量开发的设计.Vue 的核心库只关注视图层,它不仅易于上手,还便于与第三方库或既有项目整合. 数据渲染机制: 核心: 响应式数据绑定 ...
- MacOS系統Flutter打包apk
一.打包前需要做一些基本设置的确认 1.应用名 2.权限设置 3.applicationId:应用唯一标识符 4.versionCode:版本号 5.versionName:版本名称 6.APP应用图 ...
- [洛谷P3205] HNOI2010 合唱队
问题描述 为了在即将到来的晚会上有更好的演出效果,作为AAA合唱队负责人的小A需要将合唱队的人根据他们的身高排出一个队形.假定合唱队一共N个人,第i个人的身高为Hi米(1000<=Hi<= ...
- python基本数据预处理语法函数(2)
1.字符串格式化方法format的用法: < ^ > #分别为左对齐.居中.右对齐 '{:>18,.2f}'.format(70305084.0) #:冒号+空白填充+右对齐+固定宽 ...
- oracle中where子句和having子句中的区别
1.where 不能放在GROUP BY 后面2.HAVING 是跟GROUP BY 连在一起用的,放在GROUP BY 后面,此时的作用相当于WHERE3.WHERE 后面的条件中不能有聚集函数 ...
- log4j file 路径
默认以System.getProperty("user.dir")为准 用LOGGER.warn(System.getProperty("user.dir")) ...
- html5: postMessage解决跨域通信的问题
效果图 postmessage解析 HTML5提供了新型机制PostMessage实现安全的跨源通信. 语法 otherWindow.postMessage(message, targetOrigin ...
- python中私有属性的访问
class MyClass(): def __init__(self): self.__superprivate = "Hello" self.__semiprivate = &q ...
- Not a Number (NaN)
NaN can be produced by: 1. 0/0 2. Inf - Inf 3. Inf/Inf 4. 0*Inf 5. rem(x,y), where y=0 or x=Inf
- Jquery浅析
目录 jquery 通过jquery改变标签字体颜色 jquery和js对象之间值转化 Jquery基本选择器 Jquery层级选择器 基本筛选器 操作类属性 模太框 表单筛选器 筛选器方法 设置多个 ...