今天学习了一下Spring的BeanPostProcessor接口,该接口作用是:如果我们需要在Spring容器完成Bean的实例化,配置和其他的初始化后添加一些自己的逻辑处理,我们就可以定义一个或者多个BeanPostProcessor接口的实现。
下面我们来看一个简单的例子:
package com.spring.test.di;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
publicclass BeanPostPrcessorImpl
implements BeanPostProcessor {
// Bean
实例化之前进行的处理
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("对象"
+ beanName + "开始实例化");
return bean;
}
// Bean
实例化之后进行的处理
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("对象"
+ beanName + "实例化完成");
return bean;
}
}
只要将这个BeanPostProcessor接口的实现定义到容器中就可以了,如下所示:
<beanclass="com.spring.test.di.BeanPostPrcessorImpl"/>
测试代码如下:
package com.spring.test.di;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
publicclass TestMain {
/**
*@paramargs
*/
publicstaticvoid main(String[] args)
throws Exception {
//
得到ApplicationContext对象
ApplicationContext ctx =
new FileSystemXmlApplicationContext(
"applicationContext.xml");
//
得到Bean
ctx.getBean("logic");
}
}
运行以上测试程序,可以看到控制台打印结果:
对象logic开始实例化
对象logic实例化完成
BeanPostProcessor的作用域是容器级的,它只和所在容器有关。如果你在容器中定义了BeanPostProcessor,它仅仅对此容器中的bean进行后置。它不会对定义在另一个容器中的bean进行任何处理。
注意的一点:
BeanFactory
和ApplicationContext
对待bean后置处理器稍有不同。ApplicationContext
会自动检测在配置文件中实现了BeanPostProcessor
接口的所有bean,并把它们注册为后置处理器,然后在容器创建bean的适当时候调用它。部署一个后置处理器同部署其他的bean并没有什么区别。而使用BeanFactory
实现的时候,bean
后置处理器必须通过下面类似的代码显式地去注册:
BeanPostPrcessorImpl beanPostProcessor =
new BeanPostPrcessorImpl();
Resource resource = new FileSystemResource("applicationContext.xml");
ConfigurableBeanFactory factory =
new XmlBeanFactory(resource);
factory.addBeanPostProcessor(beanPostProcessor);
factory.getBean("logic");
分享到:
相关推荐
本篇文章主要介绍了Spring BeanPostProcessor接口使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
BeanPostProcessor接口的实现类如何被注册到Spring容器2.更改bean的定义(BeanFactoryPostProcessor接口)3.注册Bean到Spring容器(BeanDefinitionRegistryPostProcessor接口)4.通过FactoryBean结合...
实现了FactoryBean接口的类,可以通过实现getObject方法,实现加载我们想要的BeanPostProcessor接口BeanPostProcess
5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 5.4.2.1. 注册用户自定义的PropertyEditor ...
Spring学习演示 看spring原始码,加深印象,手写一些简单的spring原始码,例如transaction等。 2019-07-16根据Spring BeanPostProcessor接口,代理模式实现一个简单的AOP事务。
5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP...
5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP...
Spring AOP多种代理机制相关核心类介绍先介绍一些Spring Aop中一些核心类,大致分为三类: advisorCreator ,从spring ioc的扩展接口beanPostProcessor继承,主要用作扫描获取advisor。 advisor :顾问的意思,封装...
使用BeanPostProcessor自定义beanBeanPostProcessor接口定义了回调方法,您可以实现这些回调方法来提供您自己的(或覆盖容器的默认
5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP...
BeanPostProcessor接口定义了回调方法,您可以实现提供自己的(或覆盖容器的默认)实例化逻辑,依赖性解析逻辑,等等。如果你想实现一些自定义逻辑Spring容器实例化完成后,配置和初始化一个bean,您可以插入一个或多个...
5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP...
(6)如果BeanPostProcessor和Bean关联,则Spring将调用该接口的预初始化方法postProcessBeforeInitialzation()对Bean进行加工操作,这个非常重要,Spring的AOP就是用它实现的。 (7)如果Bean实现了...
同时,Spring还提供了扩展点(如BeanPostProcessor和InitializingBean接口),允许开发人员在Bean生命周期的不同阶段进行自定义操作。 AOP(面向切面编程)功能:Spring框架提供了AOP功能,通过切面(Aspect)来...
SSH笔记-IOC容器中 Bean 的生命周期,通过实现BeanPostProcessor接口,来定义初始化方法和销毁方法时的逻辑
提高核心竞争力,那么本套视频课程带你深入理解MyBatis、Spring及SpringMVC框架执行流程、底层原理、实现源码,让你对经典流行开源框架做到知其然亦知其所以然,让知识真正地学以致用。 其中有大量一线大厂高频面试...