99import org .springframework .context .annotation .Configuration ;
1010import org .springframework .core .env .Environment ;
1111import org .springframework .core .io .ClassPathResource ;
12+ import org .springframework .jdbc .datasource .DataSourceTransactionManager ;
13+ import org .springframework .transaction .PlatformTransactionManager ;
1214import top .cadecode .common .datasource .DynamicDataSource ;
1315import top .cadecode .common .datasource .DynamicDataSource .DynamicDataSourceHolder ;
1416
@@ -48,16 +50,12 @@ public DynamicDataSource dynamicDataSource() {
4850 Map <String , Object > configMap = this .checkDataSourceMap (dataSourceMap , key );
4951 // 取出数据源类型和 isDefault
5052 String type = (String ) configMap .get ("type" );
51- Boolean isDefault = (Boolean ) configMap .get ("isDefault" );
52- // 生成数据源实例
53- DataSource dataSource = (DataSource ) Binder .get (environment )
54- .bind (key + ".config" , this .checkDataSourceType (type , key )).get ();
53+ // 配置数据源
54+ DataSource dataSource = this .setTargetDataSourceMap (key , type );
5555 targetDataSourceMap .put (key , dataSource );
5656 // 判断是否是默认数据源
57- if (isDefault != null && isDefault .equals (true ) && !dynamicDataSource .hasDefaultDataSource ()) {
58- dynamicDataSource .setDefaultTargetDataSource (dataSource );
59- log .info ("设置默认数据源为 " + key );
60- }
57+ Boolean isDefault = (Boolean ) configMap .get ("isDefault" );
58+ this .setDefaultDataSource (dynamicDataSource , isDefault , dataSource , key );
6159 });
6260 // 判断是否设置默认数据源
6361 if (!dynamicDataSource .hasDefaultDataSource ()) {
@@ -68,6 +66,12 @@ public DynamicDataSource dynamicDataSource() {
6866 return dynamicDataSource ;
6967 }
7068
69+ @ Bean
70+ public PlatformTransactionManager transactionManager () {
71+ return new DataSourceTransactionManager (dynamicDataSource ());
72+ }
73+
74+
7175 /**
7276 * 校验 dataSourceMap
7377 */
@@ -79,19 +83,38 @@ private Map<String, Object> checkDataSourceMap(Map<String, Object> dataSourceMap
7983 }
8084
8185 /**
82- * 获取数据源 class
86+ * 设置数据源
8387 */
84- private Class <?> checkDataSourceType (String type , String key ) {
85- Class <? > dataSourceClass ;
88+ private DataSource setTargetDataSourceMap (String key , String type ) {
89+ Class <DataSource > dataSourceClass ;
8690 try {
87- dataSourceClass = Class .forName (type );
91+ dataSourceClass = ( Class < DataSource >) Class .forName (type );
8892 // 判断是否是 DataSource 类型
8993 if (!DataSource .class .isAssignableFrom (dataSourceClass )) {
9094 throw new IllegalArgumentException ("数据源 " + key + " 的类型 " + type + " 不合适" );
9195 }
96+ // 生成数据源实例
97+ DataSource dataSource = (DataSource ) Binder .get (environment )
98+ .bind (key + ".config" , dataSourceClass ).get ();
99+ return dataSource ;
92100 } catch (ClassNotFoundException e ) {
93101 throw new IllegalArgumentException ("数据源 " + key + " 的类型 " + type + " 不存在" );
102+ } catch (Exception e ) {
103+ throw new IllegalArgumentException ("创建数据源 " + key + " 时出现绑定错误" );
104+ }
105+ }
106+
107+ /**
108+ * 配置默认数据源
109+ */
110+ private void setDefaultDataSource (DynamicDataSource dynamicDataSource , Boolean isDefault ,
111+ DataSource dataSource , String key ) {
112+ if (dynamicDataSource .hasDefaultDataSource ()) {
113+ return ;
114+ }
115+ if (isDefault != null && isDefault .equals (true )) {
116+ dynamicDataSource .setDefaultTargetDataSource (dataSource );
117+ log .info ("设置默认数据源为 " + key );
94118 }
95- return dataSourceClass ;
96119 }
97120}
0 commit comments