LifecycleBeanPostProcessor的作用
LifecycleBeanPostProcessor的作用
- 概述:LifecycleBeanPostProcessor和DefaultAdvisorAutoProxyCreator
- DefaultAdvisorAutoProxyCreator
- BeanPostProcessor
- LifecycleBeanPostProcessor
- 总结
概述:LifecycleBeanPostProcessor和DefaultAdvisorAutoProxyCreator
在Spring和Shiro进行整合时,配置某些bean的时候都需要配置LifecycleBeanPostProcessor。有一次在使用@RequiresPermissions注解的时候配置了一个AuthorizationAttributeSourceAdvisor然后用到了这两个类,就仔细研究了一下。基本配置如下:
<bean id="beanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="beanPostProcessor"/>
DefaultAdvisorAutoProxyCreator
DefaultAdvisorAutoProxyCreator 实现了 BeanProcessor(利用反射实例化对象,完成bean 的实例化)接口,ApplicationContext 读取完所有的Bean 配置信息后,这个类扫描上下文寻找所有的 Advisor(一个切入点和一个通知的组成)然后把这些 Advisor 应用到所有符合切入点的 Bean中。 其中 depend-on 就指定了实现哪个 BeanProcessor 接口。因此需要还需要配置一个实现BeanPostProcessor接口的类。
BeanPostProcessor
首先来看 BeanPostProcessor:
BeanPostProcessor在 Spring 完成 Bean 的实例化、配置和其他初始化之后,如果想要添加一些自定义的逻辑处理(因此BeanPostProcessor也称为Bean后置处理器) 就需要实现一个或者多个 BeanPostProcessor 接口,注册到Spring容器中。
实现 BeanPostProcessor 主要是两个方法:
postProcessBeforeInitialization和postProcessAfterInitialization
public interface BeanPostProcessor {
@Nullable
//bean初始化之前进行处理
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Nullable
//bean初始化之后进行处理
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
LifecycleBeanPostProcessor
LifecycleBeanPostProcessor 实现了 BeanPostProcessor 接口。在 LifecycleBeanPostProcessor 类中实现了 DestructionAwareBeanPostProcessor,因此除了上述两个方法之外还多了一个 postProcessBeforeDestruction 方法,表示在bean销毁之前进行某种处理。
DestructionAwareBeanPostProcessor:
public interface DestructionAwareBeanPostProcessor extends BeanPostProcessor {
void postProcessBeforeDestruction(Object var1, String var2) throws BeansException;
default boolean requiresDestruction(Object bean) {
return true;
}
}
LifecycleBeanPostProcessor :
public class LifecycleBeanPostProcessor implements DestructionAwareBeanPostProcessor, PriorityOrdered {
//......一些属性和构造器
public Object postProcessBeforeInitialization(Object object, String name) throws BeansException {
if (object instanceof Initializable) {
try {
if (log.isDebugEnabled()) {
log.debug("Initializing bean [" + name + "]...");
}
((Initializable)object).init();
} catch (Exception var4) {
throw new FatalBeanException("Error initializing bean [" + name + "]", var4);
}
}
return object;
}
public Object postProcessAfterInitialization(Object object, String name) throws BeansException {
return object;
}
public void postProcessBeforeDestruction(Object object, String name) throws BeansException {
if (object instanceof Destroyable) {
try {
if (log.isDebugEnabled()) {
log.debug("Destroying bean [" + name + "]...");
}
((Destroyable)object).destroy();
} catch (Exception var4) {
throw new FatalBeanException("Error destroying bean [" + name + "]", var4);
}
}
}
。。。。。。省略
}
总结
可见 LifecycleBeanPostProcessor 就是通过上述三个方法对Initializable和Destroyable这两个类的init方法和destroy方法进行内部调用来实现bean 的生命周期控制。
还没有评论,来说两句吧...