代理的動機
AT 模式下
- 執行 undo-log 回滾日誌
代理的是 DateSource
這個類
手動代理
即手動注入一個DataSourceProxy,如下
@Bean
public DataSource druidDataSource() {
return new DruidDataSource()
}
// 這裏會返回名字爲 "dataSource" 的 Bean, 這裏
@Primary
@Bean("dataSource")
public DataSourceProxy dataSource(DataSource druidDataSource) {
return new DataSourceProxy(druidDataSource);
}
即使在生成這個類的時候,手動返回一個 proxy
自動代理
針對DataSource
創建一個代理類,在代理類裏面基於DataSource
獲取DataSourceProxy
(如果沒有就創建),然後調用DataSourceProxy的相關方法。核心邏輯在SeataAutoDataSourceProxyCreator
中
public class SeataAutoDataSourceProxyCreator extends AbstractAutoProxyCreator {
...
}
其中 AbstractAutoProxyCreator
來自 spring-aop
包
其他 : 自動代理是如何調用的呢 ?
本人的項目是 spring cloud
的依賴如下
<!-- 阿里相關依賴 -->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
這裏使用的 seata 版本是 1.7
, 之前用 1.6 以及 1.4 有很多 bug , 從官網可以看到 1.7
作爲一個大版本更新 ,更新了很多bug提升了性能.
回到上面的問題 : 自動代理是如何調用的呢 ?
即是 SeataAutoDataSourceProxyCreator
是如何給調用的呢 ? 在 seata-spring-boot-starter
這裏依賴裏面有一個 SeataDataSourceAutoConfiguration
, 該類將會引入 SeataAutoDataSourceProxyCreator
完成代理 .
@ConditionalOnBean({DataSource.class})
@ConditionalOnExpression("${seata.enabled:true} && ${seata.enableAutoDataSourceProxy:true} && ${seata.enable-auto-data-source-proxy:true}")
@AutoConfigureOrder(2147483647)
@AutoConfigureAfter(
value = {SeataCoreAutoConfiguration.class},
name = {"org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration"}
)
public class SeataDataSourceAutoConfiguration {
public SeataDataSourceAutoConfiguration() {
}
// 這裏注入的
@Bean({"seataAutoDataSourceProxyCreator"})
@ConditionalOnMissingBean({SeataAutoDataSourceProxyCreator.class})
public static SeataAutoDataSourceProxyCreator seataAutoDataSourceProxyCreator(SeataProperties seataProperties) {
return new SeataAutoDataSourceProxyCreator(seataProperties.isUseJdkProxy(), seataProperties.getExcludesForAutoProxying(), seataProperties.getDataSourceProxyMode());
}
}