【spring】BeanPostProcessor简介
2016-07-06 13:40
330 查看
BeanPostProcessor的用途:
如果我们需要在spring容器完成bean的实例化、配置和初始化前后添加一些自己的处理逻辑,那么可以扩展一个BeanPostProcessor接口的实现,然 后注册到容器中。
BeanPostProcessor中的方法:
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
BeanPostProcessor的用法:
顾名思义postProcessBeforeInitialization方法会在bean初始化之前调用,比如会在一个实现了InitializingBean接口的afterPropertiesSet方法或者ini-method方法之前调用,而postProcessAfterInitialization会在bean初始化之后调用。我们来举一个具体的例子,首先创建一个实体User,这个是实体包含了
{}块、无参构造方法、afterPropertiesSet方法、ini方法,将User和一个BeanPostProcessor实现类配置到spring文件中后启动容器,方法执行的先后顺序如下
user's {} block.
user's constructor.
execute postProcessBeforeInitialization.
user's afterPropertiesSet method.
user's ini method.
execute postProcessAfterInitialization.
关于InstantiationAwareBeanPostProcessor:
InstantiationAwareBeanPostProcessor是BeanPostProcessor一个子接口,它与bean的实例化相关,它在BeanPostProcessor的基础上增加了如下几个方法:
Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException;
boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException;
PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)throws BeansException
顾名思义postProcessBeforeInstantiation会在bean的实例化之前调用,postProcessAfterInstantiation、postProcessPropertyValues会在bean的实例化之后调用,接着上面的例子把User和一个InstantiationAwareBeanPostProcessor的实现类配置到sprin文件中后启动容器,方法执行的先后顺序如下
execute postProcessBeforeInstantiation
user's {} block.
user's constructor.
execute postProcessAfterInstantiation
execute postProcessPropertyValues
execute postProcessBeforeInitialization
user's afterPropertiesSet method.
user's ini method.
execute postProcessAfterInitialization
如果我们需要在spring容器完成bean的实例化、配置和初始化前后添加一些自己的处理逻辑,那么可以扩展一个BeanPostProcessor接口的实现,然 后注册到容器中。
BeanPostProcessor中的方法:
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
BeanPostProcessor的用法:
顾名思义postProcessBeforeInitialization方法会在bean初始化之前调用,比如会在一个实现了InitializingBean接口的afterPropertiesSet方法或者ini-method方法之前调用,而postProcessAfterInitialization会在bean初始化之后调用。我们来举一个具体的例子,首先创建一个实体User,这个是实体包含了
{}块、无参构造方法、afterPropertiesSet方法、ini方法,将User和一个BeanPostProcessor实现类配置到spring文件中后启动容器,方法执行的先后顺序如下
user's {} block.
user's constructor.
execute postProcessBeforeInitialization.
user's afterPropertiesSet method.
user's ini method.
execute postProcessAfterInitialization.
关于InstantiationAwareBeanPostProcessor:
InstantiationAwareBeanPostProcessor是BeanPostProcessor一个子接口,它与bean的实例化相关,它在BeanPostProcessor的基础上增加了如下几个方法:
Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException;
boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException;
PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)throws BeansException
顾名思义postProcessBeforeInstantiation会在bean的实例化之前调用,postProcessAfterInstantiation、postProcessPropertyValues会在bean的实例化之后调用,接着上面的例子把User和一个InstantiationAwareBeanPostProcessor的实现类配置到sprin文件中后启动容器,方法执行的先后顺序如下
execute postProcessBeforeInstantiation
user's {} block.
user's constructor.
execute postProcessAfterInstantiation
execute postProcessPropertyValues
execute postProcessBeforeInitialization
user's afterPropertiesSet method.
user's ini method.
execute postProcessAfterInitialization
相关文章推荐
- java使用Properties类读取配置文件信息
- MyEclipse & eclipse 安装 Freemarker Editor 插件
- java 设计模式——结构型模式(1.适配器模式)
- Spring jar包详解
- [2016/07/06] LeetCode / Java - Day 13 -
- Java中如何获取Spring中配置的bean
- MVP中RxJava的使用
- java-ContextClassLoader的意义
- Java基础进阶整理
- java1.4升级到java1.5 新特性
- mybatis-spring不能加载属性文件内容报:Cannot load JDBC driver class ${jdbc.driverClassName}'
- [JAVA]计算两个日期相差的天数
- Struts2常用标签
- 冒泡排序——java
- 超越之MongDB系列教程(九)MongDB的java驱动与Spring的整合
- Java 日期工具类 (可以直接调用)
- Java 关系运算符、逻辑运算符和三元运算符,以及运算符优先级
- JAVA程序开发参考手册
- java环境配置(win10)
- 使用优先队列实现游戏公告模块(Java)