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的使用的更多相关文章

  1. spring(三) spring事务操作

    前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...

  2. Spring(三) Spring IOC

    Spring 核心之 IOC 容器 再谈 IOC 与 DI IOC(Inversion of Control)控制反转:所谓控制反转,就是把原先我们代码里面需要实现的对象创 建.依赖的代码,反转给容器 ...

  3. Spring(三) Spring IOC 初体验

    Web IOC 容器初体验 我们还是从大家最熟悉的 DispatcherServlet 开始,我们最先想到的还是 DispatcherServlet 的 init() 方法.我们发现在 Dispath ...

  4. spring中BeanPostProcessor之一:InstantiationAwareBeanPostProcessor(01)

    在spring中beanPostProcessor绝对是开天辟地的产物,给了程序员很多自主权,beanPostProcessor即常说的bean后置处理器. 一.概览 先来说下Instantiatio ...

  5. spring中BeanPostProcessor之三:InitDestroyAnnotationBeanPostProcessor(01)

    在<spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)>一文中,分析到在调用CommonAnnotationB ...

  6. spring中BeanPostProcessor之四:AutowiredAnnotationBeanPostProcessor(01)

    在<spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)>中分析了CommonAnnotationBeanPos ...

  7. Spring中BeanPostProcessor

    Spring中BeanPostProcessor 前言: 本文旨在介绍Spring动态配置数据源的方式,即对一个DataSource的配置诸如jdbcUrl,user,password,driverC ...

  8. spring(三、spring中的eheche缓存、redis使用)

    spring(三.spring中的eheche缓存.redis使用) 本文主要介绍为什么要构建ehcache+redis两级缓存?以及在实战中如何实现?思考如何配置缓存策略更合适?这样的方案可能遗留什 ...

  9. Spring Boot2 系列教程(三)理解 Spring Boot 项目中的 parent

    前面和大伙聊了 Spring Boot 项目的三种创建方式,这三种创建方式,无论是哪一种,创建成功后,pom.xml 坐标文件中都有如下一段引用: <parent> <groupId ...

  10. Spring(三)Bean继续入门

    一.Aware相关接口 对于应用程序来说,应该尽量减少对Sping Api的耦合程度,然而有些时候为了运用Spring所提供的一些功能,有必要让Bean了解Spring容器对其进行管理的细节信息,如让 ...

随机推荐

  1. Google Capture The Flag 2018 (Quals) - Reverse - Beginner's Quest - Gatekeeper

    参考链接:https://ctftime.org/task/6264 题目 It's a media PC! All fully purchased through the online subscr ...

  2. eclipse没有Web项目和Server选项

    (1)在Eclipse中菜单help选项中选择install new software选项 (2)在work with 栏中输入 http://download.eclipse.org/release ...

  3. Sass @warn

    @warn 和 @debug 功能类似,用来帮助我们更好的调试 Sass.如: @mixin adjust-location($x, $y) { @if unitless($x) { @warn &q ...

  4. 原生jdbc操作

    1:加入dbcp连接池依赖 <dependency> <groupId>org.apache.commons</groupId> <artifactId> ...

  5. css 响应式(媒介查询)

    1.CSS 来实现响应式 CSS实现响应式网站的布局要用到的就是CSS中的媒体查询接下来来简单介绍一下: @media 类型 and (条件1) and (条件二){css样式} <link r ...

  6. Dump文件的生成

    一.Windows系统的任务管理器里抓dump 启动任务管理器,选中某个进程,右键,弹出菜单"创建转储文件" 注意事项: 当你在64位Windows系统上抓32位进程的dmup文件 ...

  7. Flask学习笔记03之路由

    1. endpoint from flask import Flask, url_for # 实例化一个Flask对象 app = Flask(__name__) # 打印默认配置信息 # 引入开发环 ...

  8. C++创建对象时什么时候用*,什么时候不用*

    用*, 表示创建的是一个指针对象,而指针的创建,必须初始化,C++中用new关键字开辟内存. 另外指针对象访问成员变量用-> , 非指针用. 就这么个原则 但是指针也可以不用-> 例如 ( ...

  9. python 全栈开发,Day9(函数的初始,返回值,传参,三元运算)

    一.函数的初始 比如python没有len()方法,如果求字符串的长度 使用for循环 s = 'asdfadsf' count = 0 for i in s: count += 1 print(co ...

  10. python爬虫学习之路-遇错笔记-1

    当在运行爬虫时同时开启了Fidder解析工具时(此爬虫并不是用于爬取手机端那内容,而是爬去电脑访问的网页时),访问目标站点会遇到以下错误: File "C:\Users\litao\AppD ...