Spring---事务-@Transactional
/** * 【代理对象创建】 * * org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator * org.springframework.beans.factory.config.BeanPostProcessor implementation that wraps each eligible bean with an AOP proxy, BeanPostProcessor的实现类,包装了每个有资格代理的Bean, * delegating to specified interceptors before invoking the bean itself. 委托指定的interceptors调用; * * public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware { * * @Nullable * protected static final Object[] DO_NOT_PROXY = null; * * @Override * public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) { * if (bean != null) { * Object cacheKey = getCacheKey(bean.getClass(), beanName); * if (this.earlyProxyReferences.remove(cacheKey) != bean) { * return wrapIfNecessary(bean, beanName, cacheKey); * } * } * return bean; * } * * protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) { * ... * // Create proxy if we have advice. * Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null); // 获取当前Bean所有的AOP advices * if (specificInterceptors != DO_NOT_PROXY) { // 当前Bean有aop advice, 执行aop织入 * this.advisedBeans.put(cacheKey, Boolean.TRUE); * Object proxy = createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean)); * this.proxyTypes.put(cacheKey, proxy.getClass()); * return proxy; * } * * this.advisedBeans.put(cacheKey, Boolean.FALSE); * return bean; * } * * protected Object createProxy(Class<?> beanClass, @Nullable String beanName, @Nullable Object[] specificInterceptors, TargetSource targetSource) { * ... * ProxyFactory proxyFactory = new ProxyFactory(); * ... * return proxyFactory.getProxy(getProxyClassLoader()); * } * } * * org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator * Generic auto proxy creator that builds AOP proxies for specific beans based on detected Advisors for each bean. 为每个Bean检测到的advisor 创建代理对象的 创建者; * Subclasses may override the findCandidateAdvisors() method to return a custom list of Advisors applying to any object. 子类 可能覆盖findCandidateAdvisors方法 返回advisors; * Advisors or advices requiring ordering should implement the org.springframework.core.Ordered interface. advisors要求顺序的需要实现Ordered接口; * This class sorts Advisors by Ordered order value. 该类将对实现Ordered接口的advice 进行排序; * Advisors that don't implement the Ordered interface will be considered as unordered; 未实现Ordered的advice将视为无序; * they will appear at the end of the advisor chain in undefined order. * * public abstract class AbstractAdvisorAutoProxyCreator extends AbstractAutoProxyCreator { * protected ListfindCandidateAdvisors() {} * protected List sortAdvisors(List advisors) {} * } * * org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator * public class AspectJAwareAdvisorAutoProxyCreator extends AbstractAdvisorAutoProxyCreator { * * } * * org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator * AspectJAwareAdvisorAutoProxyCreator subclass that processes all AspectJ annotation aspects in the current application context, as well as Spring Advisors. * AspectJAwareAdvisorAutoProxyCreator子类处理 所有的在当前应用中的AspectJ注解 和 Spring的advisors; * * public class AnnotationAwareAspectJAutoProxyCreator extends AspectJAwareAdvisorAutoProxyCreator { * * } * * 创建思路:BeanPostProcessor#postProcessAfterInitialization * * org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean * * -> * * org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsAfterInitialization * * -> * * org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#postProcessAfterInitialization{ * * public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) { * ... * return wrapIfNecessary(bean, beanName, cacheKey); * ... * } * * protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) { * ... * Object proxy = createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean)); * ... * } * * protected Object createProxy(Class<?> beanClass, @Nullable String beanName, @Nullable Object[] specificInterceptors, TargetSource targetSource) { * ... * ProxyFactory proxyFactory = new ProxyFactory(); * ... * return proxyFactory.getProxy(getProxyClassLoader()); * } * } * * BeanPostProcessor : org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator * * advice : org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor * */