1Springboot之事务&aop的核心功能源码分析
Posted 且听风吟0220
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1Springboot之事务&aop的核心功能源码分析相关的知识,希望对你有一定的参考价值。
1、AOP与事务之间的区别与联系
引入AOP的自动装配类为AopAutoConfiguration
,引入事务的自动装配类为TransactionAutoConfiguration
。
AOP功能是依赖spring-boot-starter-aop
提供的。事务功能是依赖spring-tx
提供的。
Aop & 事务transaction 中涉及需要的代理bean包括:
- Aop涉及的代理bean主要涉及切点pointcut指定的类。
- 事务中涉及的代理bean主要涉及存在@transaction的类、存在该注解的方法所在的类。
1.1、AopAutoConfiguration
通过注解EnableAspectJAutoProxy
引入ImportBeanDefinitionRegistrar
类型的AspectJAutoProxyRegistrar
。AspectJAutoProxyRegistrar目的就是引入AOP相关的后置处理器之AnnotationAwareAspectJAutoProxyCreator
。
1.2、TransactionAutoConfiguration
通过注解EnableTransactionManagement
引入ImportSelector
类型的TransactionManagementConfigurationSelector
。TransactionManagementConfigurationSelector目的就是引入事务相关的后置处理器之AutoProxyRegistrar
、ProxyTransactionManagementConfiguration
,其中AutoProxyRegistrar目的是引入InfrastructureAdvisorAutoProxyCreator
,ProxyTransactionManagementConfiguration是为了引入事务相关的环绕通知TransactionInterceptor
& 切面BeanFactoryTransactionAttributeSourceAdvisor
& 连接点TransactionAttributeSourcePointcut。
事务相关的切面BeanFactoryTransactionAttributeSourceAdvisor其IOC容器中的bean为org.springframework.transaction.config.internalTransactionAdvisor。
1.3、AopConfigUtils
在类的核心方法是初始化抽象类AbstractAutoProxyCreator的子类。其子类都是通过自动装配类间接引入的。并且是在加载完所有自动装配类之后,进一步完成其对应的BeanDefinitions时触发这些后置处理器。
public abstract class AopConfigUtils
public static final String AUTO_PROXY_CREATOR_BEAN_NAME =
"org.springframework.aop.config.internalAutoProxyCreator";
static
APC_PRIORITY_LIST.add(InfrastructureAdvisorAutoProxyCreator.class);
APC_PRIORITY_LIST.add(AspectJAwareAdvisorAutoProxyCreator.class);
APC_PRIORITY_LIST.add(AnnotationAwareAspectJAutoProxyCreator.class);
@Nullable
public static BeanDefinition registerAutoProxyCreatorIfNecessary(BeanDefinitionRegistry registry)
return registerAutoProxyCreatorIfNecessary(registry, null);
@Nullable
public static BeanDefinition registerAutoProxyCreatorIfNecessary(
BeanDefinitionRegistry registry, @Nullable Object source)
return registerOrEscalateApcAsRequired(InfrastructureAdvisorAutoProxyCreator.class, registry, source);
@Nullable
public static BeanDefinition registerAspectJAutoProxyCreatorIfNecessary(BeanDefinitionRegistry registry)
return registerAspectJAutoProxyCreatorIfNecessary(registry, null);
@Nullable
public static BeanDefinition registerAspectJAutoProxyCreatorIfNecessary(
BeanDefinitionRegistry registry, @Nullable Object source)
return registerOrEscalateApcAsRequired(AspectJAwareAdvisorAutoProxyCreator.class, registry, source);
@Nullable
public static BeanDefinition registerAspectJAnnotationAutoProxyCreatorIfNecessary(BeanDefinitionRegistry registry)
return registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry, null);
@Nullable
public static BeanDefinition registerAspectJAnnotationAutoProxyCreatorIfNecessary(
BeanDefinitionRegistry registry, @Nullable Object source)
return registerOrEscalateApcAsRequired(AnnotationAwareAspectJAutoProxyCreator.class, registry, source);
@Nullable
private static BeanDefinition registerOrEscalateApcAsRequired(
Class<?> cls, BeanDefinitionRegistry registry, @Nullable Object source)
if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME))
BeanDefinition apcDefinition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
if (!cls.getName().equals(apcDefinition.getBeanClassName()))
int currentPriority = findPriorityForClass(apcDefinition.getBeanClassName());
int requiredPriority = findPriorityForClass(cls);
if (currentPriority < requiredPriority)
apcDefinition.setBeanClassName(cls.getName());
return null;
RootBeanDefinition beanDefinition = new RootBeanDefinition(cls);
beanDefinition.setSource(source);
beanDefinition.getPropertyValues().add("order", Ordered.HIGHEST_PRECEDENCE);
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
registry.registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition);
return beanDefinition;
如果项目中同时存在 aop & 事务,则AspectJAutoProxyRegistrar、AutoProxyRegistrar分别通过AopConfigUtils初始化对应的后置处理器。但是由于两者后置处理器在IOC容器中的beanName均为AUTO_PROXY_CREATOR_BEAN_NAME
,通过上述方法registerOrEscalateApcAsRequired
得知该种情况下只有AOP相关的后置处理器存在。
2、AbstractAutoProxyCreator
public abstract class AbstractAutoProxyCreator implements SmartInstantiationAwareBeanPostProcessor
private BeanFactory beanFactory;
private final Map<Object, Boolean> advisedBeans = new ConcurrentHashMap<>(256);
@Override
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);
...
return 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)
if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName))
return bean;
if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey)))
return bean;
if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName))
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
...
// Create proxy if we have advice.
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);//#1
if (specificInterceptors != DO_NOT_PROXY)
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 Advisor[] buildAdvisors(@Nullable String beanName, @Nullable Object[] specificInterceptors)
// Handle prototypes correctly...
Advisor[] commonInterceptors = resolveInterceptorNames();
List<Object> allInterceptors = new ArrayList<>();
if (specificInterceptors != null)
allInterceptors.addAll(Arrays.asList(specificInterceptors));
if (commonInterceptors.length > 0)
if (this.applyCommonInterceptorsFirst)
allInterceptors.addAll(0, Arrays.asList(commonInterceptors));
else
allInterceptors.addAll(Arrays.asList(commonInterceptors));
Advisor[] advisors = new Advisor[allInterceptors.size()];
for (int i = 0; i < allInterceptors.size(); i++)
advisors[i] = this.advisorAdapterRegistry.wrap(allInterceptors.get(i));
return advisors;
Map集合类型的属性advisedBeans,其元素为不需要的代理的bean。
isInfrastructureClass:表示是否为aop相关的基础设施类。infrastructureClass包含Advice、Pointcut、Advisor、AopInfrastructureBean以及AnnotationAwareAspectJAutoProxyCreator提供的@Aspect等类。这些infrastructureClass均不需要代理处理。
- 步骤1:AbstractAdvisorAutoProxyCreator#getAdvicesAndAdvisorsForBean,获取当前bean对应的切面类Advisor以及切面类中对应的Interceptor类型的各类通知advice。
2.1、AbstractAdvisorAutoProxyCreator
public abstract class AbstractAdvisorAutoProxyCreator extends AbstractAutoProxyCreator
private BeanFactoryAdvisorRetrievalHelper advisorRetrievalHelper;
protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName,TargetSource targetSource)
List<Advisor> advisors = findEligibleAdvisors(beanClass, beanName);
if (advisors.isEmpty())
return DO_NOT_PROXY;
return advisors.toArray();
protected List<Advisor> findEligibleAdvisors(Class<?> beanClass, String beanName)
List<Advisor> candidateAdvisors = findCandidateAdvisors();//#1
List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);//#2
extendAdvisors(eligibleAdvisors);
if (!eligibleAdvisors.isEmpty())
eligibleAdvisors = sortAdvisors(eligibleAdvisors);
return eligibleAdvisors;
protected List<Advisor> findCandidateAdvisors() //#3
return this.advisorRetrievalHelper.findAdvisorBeans();
protected List<Advisor> findAdvisorsThatCanApply(
List<Advisor> candidateAdvisors, Class<?> beanClass, String beanName)
ProxyCreationContext.setCurrentProxiedBeanName(beanName);
try
return AopUtils.findAdvisorsThatCanApply(candidateAdvisors, beanClass);
finally
ProxyCreationContext.setCurrentProxiedBeanName(null);
private class BeanFactoryAdvisorRetrievalHelperAdapter extends BeanFactoryAdvisorRetrievalHelper
public BeanFactoryAdvisorRetrievalHelperAdapter(ConfigurableListableBeanFactory beanFactory)
super(beanFactory);
@Override
protected boolean isEligibleBean(String beanName)
return AbstractAdvisorAutoProxyCreator.this.isEligibleAdvisorBean(beanName);
- 步骤1:获取候选的切面类
- 如果存在事务则主要获取beanName为
org.springframework.transaction.config.internalTransactionAdvisor
的候选切面类Advisor之BeanFactoryTransactionAttributeSourceAdvisor
。该候选Advisor并非InfrastructureAdvisorAutoProxyCreator协助获取。 - 如果存在aop则由其子类AnnotationAwareAspectJAutoProxyCreator提供,如下:
protected List<Advisor> findCandidateAdvisors() 、
List<Advisor> advisors = super.findCandidateAdvisors();//AbstractAdvisorAutoProxyCreator#findCandidateAdvisors
// Build Advisors for all AspectJ aspects in the bean factory.
if (this.aspectJAdvisorsBuilder != null)
advisors.addAll(this.aspectJAdvisorsBuilder.buildAspectJAdvisors());
return advisors;
如上所示,AnnotationAwareAspectJAutoProxyCreator尝试获取选切面类Advisor以及AspectJ类型的Advisor。如果应用中同时存在事务 & Aop 则两者对应的Advisor由AnnotationAwareAspectJAutoProxyCreator提供【InfrastructureAdvisorAutoProxyCreator后置处理器因为与AnnotationAwareAspectJAutoProxyCreator的beanName均为org.springframework.aop.config.internalAutoProxyCreator
,最终只有后者存在于IOC容器中】。
- 步骤2:初始化应用中bean过程中首先会初始化所有切面类Advisor【事务 & Aop】,然后通过该步骤判断当前bean真正需要的候选Advisor。如果当前bean只有事务则过滤掉AOP相关的切面类Advisor…等。
BeanFactoryAdvisorRetrievalHelper
#findAdvisorBeans,核心逻辑主要获取候选切面类Advisor,例如BeanFactoryTransactionAttributeSourceAdvisor,例如如下:
public class BeanFactoryAdvisorRetrievalHelper
public List<Advisor> findAdvisorBeans()
String[] advisorNames = this.cachedAdvisorBeanNames;
if (advisorNames == null)
// 从IOC容器DefaultListableBeanFactory中获取Advisor相关的beanName
//例如事务相关的org.springframework.transaction.config.internalTransactionAdvisor
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))
else
advisors.add(this.beanFactory.getBean(name, Advisor.class));
return advisors;
isEligibleBean:默认返回true。InfrastructureAdvisorAutoProxyCreator后置处理器重写了该方法。
Spring系列之——Spring事务以及两大核心IOC和AOP
1 Spring事务
1.1 Spring事务是什么(百度)
事务是对一系列的数据库操作(比如插入多条数据)进行统一的提交或是回滚操作,如果插入成功,那么一起成功,如果中间一条出现异常,那么回滚之前的所有操作。
Spring事务管理机制使用的是TransactionManager进行管理。回滚注解@Transactional。
2 Spring特征
1)开源框架。
2)IOC(控制反转)
将类的创建和依赖关系写在配置文件中,由配置文件注入,实现松耦合。
3)AOP
将安全、事务等程序逻辑相对独立的功能抽取出来,利用spring的配置文件将这些功能插进去。
补充:AOP适用场景?
以上是关于1Springboot之事务&aop的核心功能源码分析的主要内容,如果未能解决你的问题,请参考以下文章