Spring框架之所以NB,BeanPostProcessor功不可没。BeanPostProcessor通过生命周期回调实现对Bean的增强比如,其实Spring的AOP功能主要就是通过BeanPostProcessor实现的,以及,Spring重要的自动装配功能,也是通过BeanPostProcessor实现的。
BeanPostProcessor生效原理
BeanPostProcessor指的是Spring容器中所有实现了BeanPostProcessor接口的类,通过如下方式生效:
- BeanPostProcessor注册:所有实现了BeanPostProcessor接口的类,在Spring初始化过程中注册到Spring容器中
- Bean生命周期的初始化前、后,Spring分别回调BeanPostProcessor接口的postProcessBeforeInitialization、postProcessAfterInitialization,从而实现对bean的增强。
具体的回调过程,在AbstractAutowireCapableBeanFactory的initializeBean方法中:
protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
invokeAwareMethods(beanName, bean);
return null;
}, getAccessControlContext());
}
else {
invokeAwareMethods(beanName, bean);
}
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
}
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}
beanPostProcessor的注册
Spring初始化时,refresh()方法中注册beanPostProcessor:
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
...
registerBeanPostProcessors方法负责注册beanPostProcessors,所有当前容器中实现了BeanPostProcessor接口的类,按照如下顺序依次注册到容器的beanPostProcessors中:
- 首先,实现了PriorityOrdered接口的BeanPostProcessor
- 其次,实现了Ordered接口的BeanPostProcessor
- 最后,其他普通BeanPostProcessor
这样,所有的BeanPostProcessor就会在bean实例化、初始化之前的Spring启动阶段注册到Spring容器中。之后的Bean初始化过程中,Spring会自动检查所有的BeanPostProcessor,并依次回调两个接口方法从而实现BeanPostProcessor对bean的增强。
此外,还可以通过编程的方式注册BeanPostProcessor:通过ConfigurableBeanFactory的addBeanPostProcessor方法注册,编程方式注册的BeanPostProcessor优先于Spring自动注册的BeanPostProcessor,并且,编程方式注册的情况下BeanPostProcessor的顺序与BeanPostProcessor的Ordered接口无关、仅与注册先后顺序有关。
AOP与BeanPostProcessor
由于Spring AOP就是通过BeanPostProcessor实现代理对象的自动创建的,所以,如果一个AOP对象同时实现了BeanPostProcessor接口,该对象的AOP功能就没有办法实现,因为该AOP对象在Spring启动的过程中作为BeanPostProcessor被注册到Spring容器中,与实现AOP动态代理的BeanPostProcessor是处于同一级别的、从而不会被AOP的BeanPostProcessor增强,因此也就无法实现AOP。
同时,需要注意的是,如果你通过@Autowired或@Resource自动装配你的BeanPostProcessor类,在没有指定bean name的情况下,有可能会发生不可预料的结果,因为自动装配过程中,如果不能通过name匹配到bean,Spring会根据type自动匹配,由于容器中有很多BeanPostProcessor,所以,可能就会发生错乱…文章来源:https://www.toymoban.com/news/detail-531049.html
上一篇 Spring FrameWork从入门到NB - 定制Bean文章来源地址https://www.toymoban.com/news/detail-531049.html
到了这里,关于Spring FrameWork从入门到NB - BeanPostProcessor的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!