BeanProcessor的理解java
BeanProcessor是spring中的一个重要接口,他有两个接口方法一个是postProcessBeforeInitialization
前置初始化,另外一个是postProcessAfterInitialization
后置初始化。从名称上就能够大概清楚这个接口的做用:在一个业务流程的先后加入两个接口方法,当执行这个业务流程时,就会触发这两个接口方法的执行。简单的总结一下有两个要点:spring
而在spring中,就有不少实现了BeanProcessor的bean,经过在重要的业务流程(如bean的生命周期流程)的先后加上BeanProcessor接口方法,就能够对业务逻辑进行修改或补充。api
一个BeanProcessor的使用实例数组
在spring的bean生命周期中,BeanProcessor接口方法会在bean建立后的初始化方法(init-method或@PostConstruct指向的方法)先后执行before
和after
方法;那有没有在bean建立先后执行的接口方法呢?答案是确定有的,这个功能是由BeanProcessor的子接口InstantiationAwareBeanPostProcessor
来实现的,他也是有before
和after
方法,会在bean实例化先后执行。缓存
咱们先定义一个BeanProcessor
接口实现类和一个InstantiationAwareBeanPostProcessor
接口实现类。app
BeanPostProcessor
实现类:ide
//net.postProcessor.CustomerPostProcessor
@Component
public class CustomerPostProcessor implements BeanPostProcessor {
@PostConstruct
public void init(){
System.out.println("执行CustomerPostProcessor的PostConstruct");
}
public CustomerPostProcessor(){
System.out.println("执行CustomerPostProcessor的构造方法");
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println(bean+"======BeforeInitialization======"+ beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println(bean+"======AfterInitialization======"+ beanName);
return bean;
}
}
复制代码
InstantiationAwareBeanPostProcessor
实现类:post
//net.postProcessor.CustomerInitialPostProcessor
@Component
public class CustomerInitialPostProcessor implements InstantiationAwareBeanPostProcessor {
@PostConstruct
public void init(){
System.out.println("执行CustomerInitialPostProcessor的PostConstruct");
}
public CustomerInitialPostProcessor(){
System.out.println("执行CustomerInitialPostProcessor的构造方法");
}
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
System.out.println("bean初始化前执行:class为"+beanClass.getName()+"|beanName为"+beanName);
return null;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
System.out.println("bean初始化后执行:Object为"+bean+"|beanName为"+beanName);
return false;
}
}
复制代码
再建立一个普通的bean对象:this
//net.postProcessor.FirstBean
@Component
public class FirstBean implements InitializingBean {
private String msg = "hello";
@PostConstruct
public void init(){
System.out.println("执行FirstBean的PostConstruct");
}
public FirstBean(){
System.out.println("FirstBean构造方法!"+msg);
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("执行FirstBean的afterPropertiesSet");
}
}
复制代码
咱们建立一个spring工厂对象将上述bean加载进去:spa
@Test
public void test(){
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext("net.postProcessor");
}
//执行获得如下结果:
执行CustomerInitialPostProcessor的构造方法
执行CustomerInitialPostProcessor的PostConstruct
执行CustomerPostProcessor的构造方法
执行CustomerPostProcessor的PostConstruct
bean初始化前执行:class为net.postProcessor.FirstBean|beanName为firstBean FirstBean构造方法!hello bean初始化后执行:Object为net.postProcessor.FirstBean@79179359|beanName为firstBean net.postProcessor.FirstBean@79179359======BeforeInitialization======firstBean
执行FirstBean的PostConstruct
执行FirstBean的afterPropertiesSet
net.postProcessor.FirstBean@79179359======AfterInitialization======firstBean
复制代码
经过上述结果证实了咱们以前的说法是正确的:
1.BeanPostProcessor接口类会优先实例化,且在实例化中没法不会调用BeanPostProcessor接口方法的
2.InstantiationAwareBeanPostProcessor接口方法会在FirstBean构造方法构造方法先后执行
3.BeanPostProcessor接口方法会在FirstBean实例化后进行初始化的先后执行
注意:若@PostConstruct注解方法方法未执行,请加入javax.annotation:javax.annotation-api:1.3.2
jar包依赖,缘由是@PostConstruct是J2EE标准的注解,不是spring本身的接口,而在JDK8往上的版本中设计者打算弃用这些注解,因此作了处理,咱们是没有办法直接使用J2EE标准注解的(@Resource、@PostConstruct、@PreDestroy等几个注解),为了兼容这种状况,因此有了javax.annotation-api
jar包的产生(或者下降JDK版本)。
BeanProcessor大概的实现思路
经过以前的了解BeanProcessor的使用,咱们能够知道BeanProcessor并不复杂,可是却十分的重要,下面来分析下BeanProcessor的实现思路:
建立个接口A,接口包含一些切点方法(Before、After、Around之类的),实现这个接口A的类要在使用前就建立好
咱们须要有个业务流程,这个业务流程由若干步组成;将接口A的接口方法插入到这些业务步骤之间(须要扩展的地方)
要执行这个业务流程时,把接口A的实现类对象赋值到业务流程中,在执行业务流程中,就会触发接口方法的执行完成功能扩展
当咱们更换赋值到业务流程中的接口A的实现类时,对应的扩展逻辑也会随之变化,这样就实现了可插拔式的扩展逻辑(策略模式)。
一个BeanProcessor的简化逻辑实例
在spring中咱们能够建立任意数量的bean实现BeanProcessor接口,因此实际上咱们是要一个全局的beanProcessorList
对象用来存储这些BeanProcessor对象;在执行业务代码时,要循环这个beanProcessorList
对象,获取你须要的BeanProcessor对象来执行接口方法。下面是一个模拟spring bean生命周期的简化版,来帮助你理解spring中BeanProcessor的工做原理。
net.postProcessor.SecondBean.java
@Component
public class SecondBean {
private String msg = "world";
public SecondBean(){
System.out.println("SecondBean构造方法!"+msg);
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
复制代码
net.postProcessor.CustomerPostProcessor.java
@Component
public class CustomerPostProcessor implements BeanPostProcessor {
@PostConstruct
public void init(){
System.out.println("执行CustomerPostProcessor的PostConstruct");
}
public CustomerPostProcessor(){
System.out.println("执行CustomerPostProcessor的构造方法");
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println(bean+"======BeforeInitialization======"+ beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println(bean+"======AfterInitialization======"+ beanName);
return bean;
}
}
复制代码
net.postProcessor.PostProcessor.java
public class PostProcessor {
//模拟扫描到的bean信息<"SecondBean", "net.postProcessor.SecondBean">
Map<String, String> scanBeanMap = new HashMap<>();
//模拟spring的beanPostProcessors列表
List<BeanPostProcessor> processorBeanList = new ArrayList<>();
//模拟bean对象缓存
Map<String, Object> beanCache = new HashMap<>();
//添加扫描的bean信息
public PostProcessor addBeanInfo(String beanName, String classPath){
this.scanBeanMap.put(beanName, classPath);
return this;
}
//模拟bean建立流程
public Object execute(){
try {
//先临时存储实现了postProcessor接口的bean对象
List<BeanPostProcessor> postProcessorStrList = new ArrayList<>();
//循环scanBeanMap,获取bean列表中实现了postProcessor接口的类,加入processorBeanList中
for(String temp: scanBeanMap.keySet()){
Class<?> clazz = Class.forName(scanBeanMap.get(temp));
//判断是否实现了BeanPostProcessor接口
if(BeanPostProcessor.class.isAssignableFrom(clazz)){
//实例化让如临时容器
postProcessorStrList.add((BeanPostProcessor)createBean(temp));
}
}
//将实现了postProcessor接口的bean加入processorBeanList中
for(BeanPostProcessor obj: postProcessorStrList){
processorBeanList.add(obj);
}
//再次循环scanBeanMap初始化所用bean
for(String temp: scanBeanMap.keySet()){
createBean(temp);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
//bean实例化
public Object createBean(String beanName){
//从缓存中获取
if(beanCache.containsKey(beanName)){
return beanCache.get(beanName);
}else{
//缓存中取不到,则进行建立后加入缓存
try {
Class<?> clazz = Class.forName(scanBeanMap.get(beanName));
//processor前置方法执行
for(BeanPostProcessor processor : processorBeanList){
processor.postProcessBeforeInitialization(clazz, beanName);
}
//bean实例化
Object result = clazz.getConstructor().newInstance();
//processor后置方法执行
for(BeanPostProcessor processor : processorBeanList){
processor.postProcessAfterInitialization(result, beanName);
}
//将bean加入缓存
beanCache.put(beanName, result);
return result;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e){
e.printStackTrace();
}
}
return null;
}
}
复制代码
代码调用
public static void main(String[] args) {
PostProcessor postProcessor = new PostProcessor();
//添加扫描到的bean
postProcessor
.addBeanInfo("SecondBean", "net.postProcessor.SecondBean")
.addBeanInfo("CustomerPostProcessor", "net.postProcessor.CustomerPostProcessor");
postProcessor.execute();
}
//执行结果
执行CustomerPostProcessor的构造方法
class net.postProcessor.SecondBean======BeforeInitialization======SecondBean
SecondBean构造方法!world
net.postProcessor.SecondBean@1b40d5f0======AfterInitialization======SecondBean
复制代码
代码逻辑以下:
循环bean信息列表,将BeanPostProcessor接口bean分离出来优先实例化(实例化中缓存bean对象),并将之放入临时容器。
循环完成,将临时容器中的BeanPostProcessor接口bean赋值到全局BeanPostProcessor接口列表中
再次循环bean信息列表,缓存存在则直接返回缓存对象,不存在则进行bean实例化,期间循环调用全局BeanPostProcessor接口对象方法
咱们要从spring中的refresh()开始看起:
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
//刷新准备
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
//告诉子类刷新内部bean工厂。
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
//为容器准备bean工程
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
//容许在上下文bean的后处理工厂子类。
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
//优先将BeanDefinitionRegistryPostProcessor\BeanFactoryPostProcessor接口的bean对象实例化
//属于spring内部组件调用
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
//处理用户自定义PostProcessor接口对象,以后加入spring的beanPostProcessors列表,
// 供以后预实例化其余bean时触发这些PostProcessor方法
registerBeanPostProcessors(beanFactory);
//...省略代码
//实例化全部(non-lazy-init)单件。
finishBeanFactoryInitialization(beanFactory);
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
//bean销毁
destroyBeans();
// Reset 'active' flag.
//取消刷新
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
//重置公共缓存
resetCommonCaches();
}
}
}
复制代码
其中包含有postProcess字段都有可能和BeanProcessor相关,这里有三个相关方法:
因此registerBeanPostProcessors()就是咱们要找的对象,来跟进看下registerBeanPostProcessors():
//AbstractApplicationContext#registerBeanPostProcessors
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
//委托给PostProcessorRegistrationDelegate.registerBeanPostProcessors进行处理
PostProcessorRegistrationDelegate.registerBeanPostProcessors进行处理(beanFactory, this);
}
复制代码
继续跟进PostProcessorRegistrationDelegate.registerBeanPostProcessors():
public static void registerBeanPostProcessors( ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
//查询实现了BeanPostProcessor接口的beanName
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
// Register BeanPostProcessorChecker that logs an info message when
// a bean is created during BeanPostProcessor instantiation, i.e. when
// a bean is not eligible for getting processed by all BeanPostProcessors.
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
// Separate between BeanPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
List<String> orderedPostProcessorNames = new ArrayList<>();
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
//根据beanName循环调用getBean进行实例化
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
priorityOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
else {
nonOrderedPostProcessorNames.add(ppName);
}
}
// First, register the BeanPostProcessors that implement PriorityOrdered.
//对BeanPostProcessor接口对象进行排序
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
//将获取到的PostProcessors接口对象加入到spring的beanPostProcessors列表
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
// Next, register the BeanPostProcessors that implement Ordered.
List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>();
for (String ppName : orderedPostProcessorNames) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
orderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
sortPostProcessors(orderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
// Now, register all regular BeanPostProcessors.
List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
for (String ppName : nonOrderedPostProcessorNames) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
nonOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
// Finally, re-register all internal BeanPostProcessors.
sortPostProcessors(internalPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, internalPostProcessors);
// Re-register post-processor for detecting inner beans as ApplicationListeners,
// moving it to the end of the processor chain (for picking up proxies etc).
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}
复制代码
果真这里就是处理BeanPostProcessor接口的地方,逻辑和以前的思路相似: