最近这段时间在学习spring中的事务相关的知识,发现还是要学习以下spring的源码
学习环境:springboot2.2.4
先从事务的使用方式开始分析:秘密都在@EnableTransactionManagement这个注解中,这个类会向spring容器中导入如下类:TransactionManagementConfigurationSelector。这也是springboot的一贯风格,@EnableXXXX注解底层都是导入一个类来实现自动注入。
这个类的核心代码如下:
protected String[] selectImports(AdviceMode adviceMode) {
switch (adviceMode) {
case PROXY:
return new String[] {AutoProxyRegistrar.class.getName(),
ProxyTransactionManagementConfiguration.class.getName()};
case ASPECTJ:
return new String[] {determineTransactionAspectClass()};
default:
return null;
}
}
其中adviceMode是PROXY(具体为什么是PROXY,可以查看方法的调用栈,这个PROXY是springboot自动配置那里传过来的)。这个类的作用还是向ioc容器中导入2个类AutoProxyRegistrar和ProxyTransactionManagementConfiguration。下面一个个来分析。
AutoProxyRegistrar这个类的作用还是向ioc中注入一个类:InfrastructureAdvisorAutoProxyCreator。(spring源码就是这么绕,还是要买本书看,自己看太晕了,抓不住重点)。InfrastructureAdvisorAutoProxyCreator这个类是事务源码中第一个重要的类。下面重点分析这个类的代码。打开这个类的UML类图,发现这个类实现BeanPostProcessor(这个接口多重要不用多说了吧)。这个类的核心方法postProcessAfterInitialization分析如下:
@Override
public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
if (bean != null) {
Object cacheKey = getCacheKey(bean.getClass(), beanName);
if (this.earlyProxyReferences.remove(cacheKey) != bean) {
//判断这个bean是否需要代理
return wrapIfNecessary(bean, beanName, cacheKey);
}
}
return bean;
}
这个方法学过Aop同学非常熟悉,这个方法最终会执行wrapIfNecessary方法,返回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.
//核心方法1 获取该bean的切面
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
if (specificInterceptors != DO_NOT_PROXY) {
this.advisedBeans.put(cacheKey, Boolean.TRUE);
//核心方法2 根据匹配的advisor创建代理对象
Object proxy = createProxy(
bean.getClass(), beanName, specificInterceptor