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. linux--基础知识1

    #进入终端窗口,root命令提示符#,普通用户登陆提示符$,切换终端用户 ctrl+shift+F2,退出终端命令exit #init 0 关机  reboot 重启  ls查看当前目录下文件  ls ...

  2. linux man命令后面各种括号的意义

    圆括号 我们经常会看到 在说一个对象的man page 的时候,会有这样的格式: mmap(2) shm_open(3) 这个后面的数字是什么意思呢,通过 man man 命令就可以知道,这个是数字是 ...

  3. css--图片整合(精灵图)

    图片整合(精灵图) 精灵图的优点: 减少图片的字节 减少了网页的http请求,从而大大的提高了页面的性能 解决了网页设计师在图片命名上的困扰,只需对一张集合的图片上命名就可以了,不需要对每一个小元素进 ...

  4. 网络安全意识有多重要?SamSam勒索软件敲诈了近600万美元

    近年来,对于网络犯罪分子来说,勒索软件已成为数百万美元的黑市业务,SamSam就是一个很好的例子. 中国信息安全新研究显示,自2015年12月以来,SamSam勒索软件从受害者手中敲诈了近600万美元 ...

  5. 5.xml约束技术--------schema

    1.schema约束 (1)dtd语法:<!ELEMENT 元素名称 约束> (2)schema符合xml的语法,xml语句 (3)一个xml文件中只能有一个dtd,但是可以有多个sche ...

  6. SQL Server数据库的软硬件性能瓶颈

    在过去十年里,很多复杂的企业应用都是用Microsoft SQL Server进行开发和部署的.如今,SQL Server已经成为现代业务应用的基石,并且它还是很多大公司业务流程的核心.SQL Ser ...

  7. 【Java】ApplicationContext应用上下文工具类

    @Slf4j @Service public class SpringContextHolder implements ApplicationContextAware, DisposableBean ...

  8. VBA在Excel中的应用(三)

    目录  Chart Export Chart Format Chart Lengend  Chart Protect  Chart Title  Chart Chart Export 1. 将Exce ...

  9. oracle系统对象

    select * from all_tab_comments-- 查询所有用户的表,视图等 select * from user_tab_comments  -- 查询本用户的表,视图等 select ...

  10. HTML-字符实体,平方米(㎡)m²

    转载自:https://blog.csdn.net/hangGe0111/article/details/80983250