提交 | 用户 | 时间
|
1ac2bc
|
1 |
/** |
懒 |
2 |
* Copyright 2018-2020 stylefeng & fengshuonan (sn93@qq.com) |
|
3 |
* <p> |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* <p> |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* <p> |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
*/ |
|
16 |
package cn.stylefeng.guns.config.datasource; |
|
17 |
|
|
18 |
import cn.stylefeng.guns.base.db.factory.AtomikosFactory; |
|
19 |
import cn.stylefeng.roses.core.config.properties.DruidProperties; |
|
20 |
import cn.stylefeng.roses.core.mutidatasource.aop.MultiSourceExAop; |
|
21 |
import org.springframework.beans.factory.annotation.Qualifier; |
|
22 |
import org.springframework.boot.context.properties.ConfigurationProperties; |
|
23 |
import org.springframework.context.annotation.Bean; |
|
24 |
import org.springframework.context.annotation.Configuration; |
|
25 |
import org.springframework.context.annotation.Primary; |
|
26 |
|
|
27 |
import javax.sql.DataSource; |
|
28 |
|
|
29 |
import static cn.stylefeng.guns.base.db.context.DataSourceContext.MASTER_DATASOURCE_NAME; |
|
30 |
|
|
31 |
/** |
|
32 |
* 多数据源配置<br/> |
|
33 |
* <p> |
|
34 |
* 注:由于引入多数据源,所以让spring事务的aop要在多数据源切换aop的后面 |
|
35 |
* |
|
36 |
* @author stylefeng |
|
37 |
* @Date 2017/5/20 21:58 |
|
38 |
*/ |
|
39 |
@Configuration |
|
40 |
public class DataSourceConfig { |
|
41 |
|
|
42 |
/** |
|
43 |
* 默认主数据源配置 |
|
44 |
*/ |
|
45 |
@Bean |
|
46 |
@Primary |
|
47 |
@ConfigurationProperties(prefix = "spring.datasource") |
|
48 |
public DruidProperties druidProperties() { |
|
49 |
return new DruidProperties(); |
|
50 |
} |
|
51 |
|
|
52 |
/** |
|
53 |
* 主数据源实例 |
|
54 |
*/ |
|
55 |
@Primary |
|
56 |
@Bean |
|
57 |
public DataSource dataSourcePrimary(@Qualifier("druidProperties") DruidProperties druidProperties) { |
|
58 |
return AtomikosFactory.create(MASTER_DATASOURCE_NAME, druidProperties); |
|
59 |
} |
|
60 |
|
|
61 |
/** |
|
62 |
* 多数据源切换的aop |
|
63 |
*/ |
|
64 |
@Bean |
|
65 |
public MultiSourceExAop multiSourceExAop() { |
|
66 |
return new MultiSourceExAop(); |
|
67 |
} |
|
68 |
|
|
69 |
} |