Spring-framework应用程序启动loadtime源码分析笔记(二)——@Transactional

@Transactional标识类或方法,使方法被执行时使用事务方式执行,这里只讨论PROXY方法增强方法。使用@EnableTransactionManagement,默认model=AdviceMode.PROXY,通过@Import(TransactionManagementConfigurationSelector.class)来判断在加载时注册PROXY不是ASPECTJ相关类。PROXY相关类有AutoProxyRegistrar和ProxyTransactionManagementConfiguration。AutoProxyRegistrar作用是通过EnableTransactionManagement.proxyTargetClass值判断使用CGLIB或是Java interface-based proxies。使用Java interface-based proxies时AnnotationAwareAspectJAutoProxyCreator,AnnotationAwareAspectJAutoProxyCreator是一个BeanPostProcessor实现类。使用CGLIB也是注册AnnotationAwareAspectJAutoProxyCreator并且将proxyTargetClass=true,如下AopConfigUtils.forceAutoProxyCreatorToUseClassProxying()
 

AnnotationAwareAspectJAutoProxyCreator.wrapIfNecessary()

AnnotationAwareAspectJAutoProxyCreator是一个BeanPostProcessor实现类,它的相应方法会在容器管理每个对象实例化不同阶段会执行。wrapIfNecessary()方法在postProcessAfterInitialization()方法调用,主要是判断实例化对象是否容器所有Advisor所要作用对象,如@Transactional标识的方法或类的对象匹配BeanFactoryTransactionAttributeSourceAdvisor。
 
AnnotationTransactionAttributeSource.determineTransactionAttribute()
判断传入的方法是否含@Transactional标识,返回TransactionAttribute
下面是AnnotationAwareAspectJAutoProxyCreator(AbstractAutoProxyCreator).wrapIfNecessary()的stack
Thread [main] (Suspended (breakpoint at line 348 in AbstractAutoProxyCreator))    
    owns: ConcurrentHashMap<K,V>  (id=2343)    
    owns: Object  (id=2107)    
    AnnotationAwareAspectJAutoProxyCreator(AbstractAutoProxyCreator).wrapIfNecessary(Object, String, Object) line: 348    
    AnnotationAwareAspectJAutoProxyCreator(AbstractAutoProxyCreator).postProcessAfterInitialization(Object, String) line: 298    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).applyBeanPostProcessorsAfterInitialization(Object, String) line: 435    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).initializeBean(String, Object, RootBeanDefinition) line: 1721    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).doCreateBean(String, RootBeanDefinition, Object[]) line: 581    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBean(String, RootBeanDefinition, Object[]) line: 498    
    DefaultListableBeanFactory(AbstractBeanFactory).lambda$doGetBean$0(String, RootBeanDefinition, Object[]) line: 305    
    48361312.getObject() line: not available    
    DefaultListableBeanFactory(DefaultSingletonBeanRegistry).getSingleton(String, ObjectFactory<?>) line: 233    
    DefaultListableBeanFactory(AbstractBeanFactory).doGetBean(String, Class<T>, Object[], boolean) line: 303    
    DefaultListableBeanFactory(AbstractBeanFactory).getBean(String, Class<T>) line: 198    
    CommonAnnotationBeanPostProcessor.autowireResource(BeanFactory, CommonAnnotationBeanPostProcessor$LookupElement, String) line: 512    
    CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor$LookupElement, String) line: 483    
    CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(Object, String) line: 617    
    CommonAnnotationBeanPostProcessor$ResourceElement(InjectionMetadata$InjectedElement).inject(Object, String, PropertyValues) line: 172    
    InjectionMetadata.inject(Object, String, PropertyValues) line: 89    
    CommonAnnotationBeanPostProcessor.postProcessPropertyValues(PropertyValues, PropertyDescriptor[], Object, String) line: 317    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).populateBean(String, RootBeanDefinition, BeanWrapper) line: 1353    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).doCreateBean(String, RootBeanDefinition, Object[]) line: 579    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBean(String, RootBeanDefinition, Object[]) line: 498    
    DefaultListableBeanFactory(AbstractBeanFactory).lambda$doGetBean$0(String, RootBeanDefinition, Object[]) line: 305    
    48361312.getObject() line: not available    
    DefaultListableBeanFactory(DefaultSingletonBeanRegistry).getSingleton(String, ObjectFactory<?>) line: 233    
    DefaultListableBeanFactory(AbstractBeanFactory).doGetBean(String, Class<T>, Object[], boolean) line: 303    
    DefaultListableBeanFactory(AbstractBeanFactory).getBean(String, Class<T>) line: 198    
    CommonAnnotationBeanPostProcessor.autowireResource(BeanFactory, CommonAnnotationBeanPostProcessor$LookupElement, String) line: 512    
    CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor$LookupElement, String) line: 483    
    CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(Object, String) line: 617    
    CommonAnnotationBeanPostProcessor$ResourceElement(InjectionMetadata$InjectedElement).inject(Object, String, PropertyValues) line: 172    
    InjectionMetadata.inject(Object, String, PropertyValues) line: 89    
    CommonAnnotationBeanPostProcessor.postProcessPropertyValues(PropertyValues, PropertyDescriptor[], Object, String) line: 317    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).populateBean(String, RootBeanDefinition, BeanWrapper) line: 1353    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).doCreateBean(String, RootBeanDefinition, Object[]) line: 579    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBean(String, RootBeanDefinition, Object[]) line: 498    
    DefaultListableBeanFactory(AbstractBeanFactory).lambda$doGetBean$0(String, RootBeanDefinition, Object[]) line: 305    
    48361312.getObject() line: not available    
    DefaultListableBeanFactory(DefaultSingletonBeanRegistry).getSingleton(String, ObjectFactory<?>) line: 233    
    DefaultListableBeanFactory(AbstractBeanFactory).doGetBean(String, Class<T>, Object[], boolean) line: 303    
    DefaultListableBeanFactory(AbstractBeanFactory).getBean(String) line: 193    
    DefaultListableBeanFactory.preInstantiateSingletons() line: 747    
    AnnotationConfigApplicationContext(AbstractApplicationContext).finishBeanFactoryInitialization(ConfigurableListableBeanFactory) line: 861    
    AnnotationConfigApplicationContext(AbstractApplicationContext).refresh() line: 542    
    SpringApplication.refresh(ApplicationContext) line: 750    
    SpringApplication.refreshContext(ConfigurableApplicationContext) line: 386    
    SpringApplication.run(String...) line: 327    
    SpringApplication.run(Class<?>[], String[]) line: 1245    
    SpringApplication.run(Class<?>, String...) line: 1233    
    Application.main(String[]) line: 56    
如果对象是Advisor所要作用对象,则调用createProxy()生成proxy对象封闭目标对象,增强目标对象。如下面stack
Thread [main] (Suspended (breakpoint at line 441 in AbstractAutoProxyCreator))    
    owns: ConcurrentHashMap<K,V>  (id=2343)    
    owns: Object  (id=2107)    
    AnnotationAwareAspectJAutoProxyCreator(AbstractAutoProxyCreator).createProxy(Class<?>, String, Object[], TargetSource) line: 441    
    AnnotationAwareAspectJAutoProxyCreator(AbstractAutoProxyCreator).wrapIfNecessary(Object, String, Object) line: 349    
    AnnotationAwareAspectJAutoProxyCreator(AbstractAutoProxyCreator).postProcessAfterInitialization(Object, String) line: 298    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).applyBeanPostProcessorsAfterInitialization(Object, String) line: 435    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).initializeBean(String, Object, RootBeanDefinition) line: 1721    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).doCreateBean(String, RootBeanDefinition, Object[]) line: 581    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBean(String, RootBeanDefinition, Object[]) line: 498    
    DefaultListableBeanFactory(AbstractBeanFactory).lambda$doGetBean$0(String, RootBeanDefinition, Object[]) line: 305    
    48361312.getObject() line: not available    
    DefaultListableBeanFactory(DefaultSingletonBeanRegistry).getSingleton(String, ObjectFactory<?>) line: 233    
    DefaultListableBeanFactory(AbstractBeanFactory).doGetBean(String, Class<T>, Object[], boolean) line: 303    
    DefaultListableBeanFactory(AbstractBeanFactory).getBean(String, Class<T>) line: 198    
    CommonAnnotationBeanPostProcessor.autowireResource(BeanFactory, CommonAnnotationBeanPostProcessor$LookupElement, String) line: 512    
    CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor$LookupElement, String) line: 483    
    CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(Object, String) line: 617    
    CommonAnnotationBeanPostProcessor$ResourceElement(InjectionMetadata$InjectedElement).inject(Object, String, PropertyValues) line: 172    
    InjectionMetadata.inject(Object, String, PropertyValues) line: 89    
    CommonAnnotationBeanPostProcessor.postProcessPropertyValues(PropertyValues, PropertyDescriptor[], Object, String) line: 317    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).populateBean(String, RootBeanDefinition, BeanWrapper) line: 1353    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).doCreateBean(String, RootBeanDefinition, Object[]) line: 579    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBean(String, RootBeanDefinition, Object[]) line: 498    
    DefaultListableBeanFactory(AbstractBeanFactory).lambda$doGetBean$0(String, RootBeanDefinition, Object[]) line: 305    
    48361312.getObject() line: not available    
    DefaultListableBeanFactory(DefaultSingletonBeanRegistry).getSingleton(String, ObjectFactory<?>) line: 233    
    DefaultListableBeanFactory(AbstractBeanFactory).doGetBean(String, Class<T>, Object[], boolean) line: 303    
    DefaultListableBeanFactory(AbstractBeanFactory).getBean(String, Class<T>) line: 198    
    CommonAnnotationBeanPostProcessor.autowireResource(BeanFactory, CommonAnnotationBeanPostProcessor$LookupElement, String) line: 512    
    CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor$LookupElement, String) line: 483    
    CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(Object, String) line: 617    
    CommonAnnotationBeanPostProcessor$ResourceElement(InjectionMetadata$InjectedElement).inject(Object, String, PropertyValues) line: 172    
    InjectionMetadata.inject(Object, String, PropertyValues) line: 89    
    CommonAnnotationBeanPostProcessor.postProcessPropertyValues(PropertyValues, PropertyDescriptor[], Object, String) line: 317    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).populateBean(String, RootBeanDefinition, BeanWrapper) line: 1353    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).doCreateBean(String, RootBeanDefinition, Object[]) line: 579    
    DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBean(String, RootBeanDefinition, Object[]) line: 498    
    DefaultListableBeanFactory(AbstractBeanFactory).lambda$doGetBean$0(String, RootBeanDefinition, Object[]) line: 305    
    48361312.getObject() line: not available    
    DefaultListableBeanFactory(DefaultSingletonBeanRegistry).getSingleton(String, ObjectFactory<?>) line: 233    
    DefaultListableBeanFactory(AbstractBeanFactory).doGetBean(String, Class<T>, Object[], boolean) line: 303    
    DefaultListableBeanFactory(AbstractBeanFactory).getBean(String) line: 193    
    DefaultListableBeanFactory.preInstantiateSingletons() line: 747    
    AnnotationConfigApplicationContext(AbstractApplicationContext).finishBeanFactoryInitialization(ConfigurableListableBeanFactory) line: 861    
    AnnotationConfigApplicationContext(AbstractApplicationContext).refresh() line: 542    
    SpringApplication.refresh(ApplicationContext) line: 750    
    SpringApplication.refreshContext(ConfigurableApplicationContext) line: 386    
    SpringApplication.run(String...) line: 327    
    SpringApplication.run(Class<?>[], String[]) line: 1245    
    SpringApplication.run(Class<?>, String...) line: 1233    
    Application.main(String[]) line: 56    
 

AbstractAutoProxyCreator.createProxy()

DefaultAopProxyFactory.createAopProxy()
@Transactional标识增强对象是类所以创建proxy对象是ObjenesisCglibAopProxy
 
 
 
posted on 2017-10-09 10:40  力奋  阅读(321)  评论(0编辑  收藏  举报