初始调用路线图:
?org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator
postProcessAfterInitialization->wrapIfNecessary->getAdvicesAndAdvisorsForBean
初始看法:
类初始化完成后调用AbstractAutoProxyCreator的Bean后处理器,扫描没有做过代理的类,对需要增强的类创建代理。
类和增强是分离的,在这实现自动匹配。
涉及到的类
1:org.springframework.aop.support.AopUtil
2:org.springframework.aop.aspectj.annotation.AspectJProxyFactory
3:org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator->{wrapIfNecessary,postProcessAfterInitialization}
4:org.springframework.test.context.junit4.SpringJUnit4ClassRunner
5:org.springframework.test.context.TestContextManager
6:org.springframework.test.context.DefaultTestContext(加载ApplicationContext)(getApplicationContext)
7:org.springframework.test.context.CacheAwareContextLoaderDelegate
8:org.springframework.transaction.annotation.SpringTransactionAnnotationParser(在这找到的突破口)
9:org.springframework.test.context.support.AbstractGenericContextLoader
10:org.springframework.aop.support.AopUtils 切面工具类(关键)
11:org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator->findAdvisorsThatCanApply
12:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory->doCreateBean
查看Spring 加载过程中加载的类
org.springframework.beans.factory.support.BeanDefinitionReaderUtils->registerBeanDefinition
AnnotationConfigUtils->registerAnnotationConfigProcessors(注解配置工具类)
事务注解:
AopAutoProxyConfigurer
configureAutoProxyCreator
AopConfigUtils->registerAutoProxyCreatorIfNecessary
事务代理类注入时机
AopConfigUtils->registerOrEscalateApcAsRequired
AopNamespaceUtils->registerAutoProxyCreatorIfNecessary
基于事务注解的bean,使用annotation定义事务
加载过程中解析过程
org.springframework.beans.factory.xml.BeanDefinitionParserDelegate->parseCustomElement
NamespaceHandlerSupport->parse->findParserForElement
org.springframework.transaction.config.AnnotationDrivenBeanDefinitionParser->parse
AopAutoProxyConfigurer->configureAutoProxyCreator
AopNamespaceUtils->registerAutoProxyCreatorIfNecessary
AopConfigUtils->registerAutoProxyCreatorIfNecessary->registerOrEscalateApcAsRequired
registry->registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition)
AUTO_PROXY_CREATOR_BEAN_NAME : org.springframework.aop.config.internalAutoProxyCreator
beanDefinition : org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator
bean 扩展接口的实现原理
AbstractAutowireCapableBeanFactory->applyBeanPostProcessorsAfterInitialization
启动步骤:
DefaultTestContext->getApplicationContext(cacheAwareContextLoaderDelegate.loadContext(mergedContextConfiguration))
->loadContextInternal(mergedContextConfiguration)->(SmartContextLoader smartContextLoader = (SmartContextLoader) contextLoader;?applicationContext = smartContextLoader.loadContext(mergedContextConfiguration);)
->org.springframework.test.context.support.AbstractGenericContextLoader->loadContext