BeanPostProcessor 理解:该接口主要作用于bean执行初始化方法前后,该接口及其子类接口不管是对于用户还是spring自身的扩展性都起到非常重要的作用.
1 2 3 4 5 6 7 8 9 10 11 12 13 public interface BeanPostProcessor { @Nullable default Object postProcessBeforeInitialization (Object bean, String beanName) throws BeansException { return bean; } @Nullable default Object postProcessAfterInitialization (Object bean, String beanName) throws BeansException { return bean; } }
MergedBeanDefinitionPostProcessor 理解:主要用于bean实例化后添加BeanDefinition信息到容器中,这个接口对@Autowired和@Value及@PostConstruct和@PreDestory的支持起到了至关重要的作用。当某个bean在实例化后就会调到所有的实现了MergedBeanDefinitionPostProcessor接口的实例。其中就有一个非常关键的类:AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor.
1 2 3 4 5 6 7 8 public interface MergedBeanDefinitionPostProcessor extends BeanPostProcessor { void postProcessMergedBeanDefinition (RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) ; default void resetBeanDefinition (String beanName) { } }
源码MergedBeanDefinitionPostProcessor接口的作用点,此处调用了postProcessMergedBeanDefinition方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 protected Object doCreateBean (String beanName, RootBeanDefinition mbd, @Nullable Object[] args) throws BeanCreationException { BeanWrapper instanceWrapper = null ; if (mbd.isSingleton()) { instanceWrapper = this .factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null ) { instanceWrapper = createBeanInstance(beanName, mbd, args); } Object bean = instanceWrapper.getWrappedInstance(); Class<?> beanType = instanceWrapper.getWrappedClass(); if (beanType != NullBean.class) { mbd.resolvedTargetType = beanType; } synchronized (mbd.postProcessingLock) { if (!mbd.postProcessed) { try { applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName); } catch (Throwable ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Post-processing of merged bean definition failed" , ex); } mbd.postProcessed = true ; } } } protected void applyMergedBeanDefinitionPostProcessors (RootBeanDefinition mbd, Class<?> beanType, String beanName) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof MergedBeanDefinitionPostProcessor) { MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp; bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName); } } }
在AutowiredAnnotationBeanPostProcessor类中用与解析扫描@Autowired和@Value注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements MergedBeanDefinitionPostProcessor , PriorityOrdered , BeanFactoryAware { public AutowiredAnnotationBeanPostProcessor () { this .autowiredAnnotationTypes.add(Autowired.class); this .autowiredAnnotationTypes.add(Value.class); try { this .autowiredAnnotationTypes.add((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Inject" , AutowiredAnnotationBeanPostProcessor.class.getClassLoader())); logger.trace("JSR-330 'javax.inject.Inject' annotation found and supported for autowiring" ); } catch (ClassNotFoundException ex) { } } @Override public void postProcessMergedBeanDefinition (RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) { InjectionMetadata metadata = findAutowiringMetadata(beanName, beanType, null ); metadata.checkConfigMembers(beanDefinition); } }
在CommonAnnotationBeanPostProcessor类中用与解析扫描@PostConstruct,@PreDestory和@Resource注解
1 2 3 4 5 6 7 8 9 10 11 12 public void postProcessMergedBeanDefinition (RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) { super .postProcessMergedBeanDefinition(beanDefinition, beanType, beanName); InjectionMetadata metadata = findResourceMetadata(beanName, beanType, null ); metadata.checkConfigMembers(beanDefinition); } public void postProcessMergedBeanDefinition (RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) { LifecycleMetadata metadata = findLifecycleMetadata(beanType); metadata.checkConfigMembers(beanDefinition); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 private LifecycleMetadata findLifecycleMetadata (Class<?> clazz) { if (this .lifecycleMetadataCache == null ) { return buildLifecycleMetadata(clazz); } LifecycleMetadata metadata = this .lifecycleMetadataCache.get(clazz); if (metadata == null ) { synchronized (this .lifecycleMetadataCache) { metadata = this .lifecycleMetadataCache.get(clazz); if (metadata == null ) { metadata = buildLifecycleMetadata(clazz); this .lifecycleMetadataCache.put(clazz, metadata); } return metadata; } } return metadata; } private LifecycleMetadata buildLifecycleMetadata (final Class<?> clazz) { if (!AnnotationUtils.isCandidateClass(clazz, Arrays.asList(this .initAnnotationType, this .destroyAnnotationType))) { return this .emptyLifecycleMetadata; } List<LifecycleElement> initMethods = new ArrayList<>(); List<LifecycleElement> destroyMethods = new ArrayList<>(); Class<?> targetClass = clazz; do { final List<LifecycleElement> currInitMethods = new ArrayList<>(); final List<LifecycleElement> currDestroyMethods = new ArrayList<>(); ReflectionUtils.doWithLocalMethods(targetClass, method -> { if (this .initAnnotationType != null && method.isAnnotationPresent(this .initAnnotationType)) { LifecycleElement element = new LifecycleElement(method); currInitMethods.add(element); if (logger.isTraceEnabled()) { logger.trace("Found init method on class [" + clazz.getName() + "]: " + method); } } if (this .destroyAnnotationType != null && method.isAnnotationPresent(this .destroyAnnotationType)) { currDestroyMethods.add(new LifecycleElement(method)); if (logger.isTraceEnabled()) { logger.trace("Found destroy method on class [" + clazz.getName() + "]: " + method); } } }); initMethods.addAll(0 , currInitMethods); destroyMethods.addAll(currDestroyMethods); targetClass = targetClass.getSuperclass(); } while (targetClass != null && targetClass != Object.class); return (initMethods.isEmpty() && destroyMethods.isEmpty() ? this .emptyLifecycleMetadata : new LifecycleMetadata(clazz, initMethods, destroyMethods)); }
InstantiationAwareBeanPostProcessor 理解:SmartInstantiationAwareBeanPostProcessor的父类接口,主要用于bean实例化前后对Bean进行处理,可以用于创建一个无需走完完整Bean的生命周期的代理对象Bean;在AOP的实现流程中,AbstractAutoProxyCreator实现了postProcessBeforeInstantiation方法,在该方法中创建Advisor对象,该对象内部持有对应切面的所有Advice(通知);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor { @Nullable default Object postProcessBeforeInstantiation (Class<?> beanClass, String beanName) throws BeansException { return null ; } default boolean postProcessAfterInstantiation (Object bean, String beanName) throws BeansException { return true ; } @Nullable default PropertyValues postProcessProperties (PropertyValues pvs, Object bean, String beanName) throws BeansException { return null ; } @Deprecated @Nullable default PropertyValues postProcessPropertyValues ( PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { return pvs; } }
在Bean实例化前,给BeanPostProcessors一个机会返回代理对象1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 protected Object createBean (String beanName, RootBeanDefinition mbd, @Nullable Object[] args) throws BeanCreationException { try { Object bean = resolveBeforeInstantiation(beanName, mbdToUse); if (bean != null ) { return bean; } } catch (Throwable ex) { throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName, "BeanPostProcessor before instantiation of bean failed" , ex); } try { Object beanInstance = doCreateBean(beanName, mbdToUse, args); if (logger.isTraceEnabled()) { logger.trace("Finished creating instance of bean '" + beanName + "'" ); } return beanInstance; } catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) { throw ex; } catch (Throwable ex) { throw new BeanCreationException( mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation" , ex); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 protected Object resolveBeforeInstantiation (String beanName, RootBeanDefinition mbd) { Object bean = null ; if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) { if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { Class<?> targetType = determineTargetType(beanName, mbd); if (targetType != null ) { bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName); if (bean != null ) { bean = applyBeanPostProcessorsAfterInitialization(bean, beanName); } } } mbd.beforeInstantiationResolved = (bean != null ); } return bean; } protected Object applyBeanPostProcessorsBeforeInstantiation (Class<?> beanClass, String beanName) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName); if (result != null ) { return result; } } } return null ; }
AbstractAutoProxyCreator实现了postProcessBeforeInstantiation方法创建Advisor对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 public Object postProcessBeforeInstantiation (Class<?> beanClass, String beanName) { Object cacheKey = getCacheKey(beanClass, beanName); if (!StringUtils.hasLength(beanName) || !this .targetSourcedBeans.contains(beanName)) { if (this .advisedBeans.containsKey(cacheKey)) { return null ; } if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) { this .advisedBeans.put(cacheKey, Boolean.FALSE); return null ; } } TargetSource targetSource = getCustomTargetSource(beanClass, beanName); if (targetSource != null ) { if (StringUtils.hasLength(beanName)) { this .targetSourcedBeans.add(beanName); } Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource); Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource); this .proxyTypes.put(cacheKey, proxy.getClass()); return proxy; } return null ; }
在shouldSkip方法中创建了Advisor
1 2 3 4 5 6 7 8 9 10 11 12 protected boolean shouldSkip (Class<?> beanClass, String beanName) { List<Advisor> candidateAdvisors = findCandidateAdvisors(); for (Advisor advisor : candidateAdvisors) { if (advisor instanceof AspectJPointcutAdvisor && ((AspectJPointcutAdvisor) advisor).getAspectName().equals(beanName)) { return true ; } } return super .shouldSkip(beanClass, beanName); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 protected List<Advisor> findCandidateAdvisors () { Assert.state(this .advisorRetrievalHelper != null , "No BeanFactoryAdvisorRetrievalHelper available" ); return this .advisorRetrievalHelper.findAdvisorBeans(); } public List<Advisor> findAdvisorBeans () { String[] advisorNames = this .cachedAdvisorBeanNames; if (advisorNames == null ) { advisorNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( this .beanFactory, Advisor.class, true , false ); this .cachedAdvisorBeanNames = advisorNames; } if (advisorNames.length == 0 ) { return new ArrayList<>(); } List<Advisor> advisors = new ArrayList<>(); for (String name : advisorNames) { if (isEligibleBean(name)) { if (this .beanFactory.isCurrentlyInCreation(name)) { if (logger.isTraceEnabled()) { logger.trace("Skipping currently created advisor '" + name + "'" ); } } else { try { advisors.add(this .beanFactory.getBean(name, Advisor.class)); } catch (BeanCreationException ex) { Throwable rootCause = ex.getMostSpecificCause(); if (rootCause instanceof BeanCurrentlyInCreationException) { BeanCreationException bce = (BeanCreationException) rootCause; String bceBeanName = bce.getBeanName(); if (bceBeanName != null && this .beanFactory.isCurrentlyInCreation(bceBeanName)) { if (logger.isTraceEnabled()) { logger.trace("Skipping advisor '" + name + "' with dependency on currently created bean: " + ex.getMessage()); } continue ; } } throw ex; } } } } return advisors; }
SmartInstantiationAwareBeanPostProcessor 理解:InstantiationAwareBeanPostProcessor的字类接口,该接口与动态代理与解决循环依赖有关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public interface SmartInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessor { @Nullable default Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException { return null ; } @Nullable default Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException { return null ; } default Object getEarlyBeanReference (Object bean, String beanName) throws BeansException { return bean; } }
DestructionAwareBeanPostProcessor 理解: 主要用于销毁对象时对Bean的处理
1 2 3 4 5 6 7 8 9 public interface DestructionAwareBeanPostProcessor extends BeanPostProcessor { void postProcessBeforeDestruction (Object bean, String beanName) throws BeansException ; default boolean requiresDestruction (Object bean) { return true ; } }
自定义InstantiationAwareBeanPostProcessor接口实现代码案例 1 2 3 4 5 6 public class BeforeInstantiation { public void doSomeThing () { System.out.println("执行do some thing ..." ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 import org.springframework.cglib.proxy.MethodInterceptor;import org.springframework.cglib.proxy.MethodProxy;import java.lang.reflect.Method;public class MyMethodInterceptor implements MethodInterceptor { @Override public Object intercept (Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { System.out.println("目标方法前:" +method); Object o1 = methodProxy.invokeSuper(o, objects); System.out.println("目标方法后:" +method); return o1; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 import org.springframework.beans.BeansException;import org.springframework.beans.PropertyValues;import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;import org.springframework.cglib.proxy.Enhancer;public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor { @Override public Object postProcessBeforeInitialization (Object bean, String beanName) throws BeansException { System.out.print("beanName:" +beanName+"执行..postProcessBeforeInitialization\n" ); return bean; } @Override public Object postProcessAfterInitialization (Object bean, String beanName) throws BeansException { System.out.print("beanName:" +beanName+"执行..postProcessAfterInitialization\n" ); return bean; } @Override public boolean postProcessAfterInstantiation (Object bean, String beanName) throws BeansException { System.out.print("beanName:" +beanName+"执行..postProcessAfterInstantiation\n" ); return false ; } @Override public Object postProcessBeforeInstantiation (Class<?> beanClass, String beanName) throws BeansException { System.out.print("beanName:" +beanName+"执行..postProcessBeforeInstantiation\n" ); if (beanClass==BeforeInstantiation.class){ Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(beanClass); enhancer.setCallback(new MyMethodInterceptor()); BeforeInstantiation beforeInstantiation = (BeforeInstantiation)enhancer.create(); System.out.print("返回动态代理\n" ); return beforeInstantiation; } return null ; } @Override public PropertyValues postProcessProperties (PropertyValues pvs, Object bean, String beanName) throws BeansException { System.out.print("beanName:" +beanName+"执行..postProcessProperties\n" ); return pvs; } }
1 2 3 4 5 6 7 8 <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <bean id="beforeInstantiation" class="com.lixiang.resolveBeforeInstantiation.BeforeInstantiation"></bean> <bean id="myInstantiationAwareBeanPostProcessor" class="com.lixiang.resolveBeforeInstantiation.MyInstantiationAwareBeanPostProcessor"></bean> </beans>
1 2 3 4 5 6 7 8 9 10 import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test { public static void main (String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("resolveBeforeInstantiation.xml" ); BeforeInstantiation bean = ac.getBean(BeforeInstantiation.class); bean.doSomeThing(); } }