Skip to content

Commit 0a58be8

Browse files
committed
增加注解方式切换数据源
1 parent 3f77ecb commit 0a58be8

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

hsweb-web-datasource/src/main/java/org/hsweb/web/datasource/dynamic/DynamicDataSourceAutoConfiguration.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
import com.atomikos.icatch.jta.UserTransactionImp;
2222
import com.atomikos.icatch.jta.UserTransactionManager;
2323
import com.atomikos.jdbc.AtomikosDataSourceBean;
24+
import org.aspectj.lang.ProceedingJoinPoint;
25+
import org.aspectj.lang.annotation.Around;
26+
import org.aspectj.lang.annotation.Aspect;
2427
import org.hsweb.commons.StringUtils;
2528
import org.hsweb.web.core.datasource.DataSourceHolder;
2629
import org.hsweb.web.core.datasource.DynamicDataSource;
30+
import org.hsweb.web.core.exception.AuthorizeForbiddenException;
2731
import org.springframework.beans.factory.annotation.Autowired;
2832
import org.springframework.beans.factory.annotation.Qualifier;
2933
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -112,4 +116,32 @@ public DynamicDataSourceSqlExecutorService sqlExecutor() {
112116
return new DynamicDataSourceSqlExecutorService();
113117
}
114118

119+
@Bean
120+
public AnnotationDataSourceChangeConfiguration annotationDataSourceChangeConfiguration() {
121+
return new AnnotationDataSourceChangeConfiguration();
122+
}
123+
124+
@Aspect
125+
public static class AnnotationDataSourceChangeConfiguration {
126+
@Around(value = "@annotation(dataSource)")
127+
public Object useDatasource(ProceedingJoinPoint pjp, UseDataSource dataSource) throws Throwable {
128+
try {
129+
DynamicDataSource.use(dataSource.value());
130+
return pjp.proceed();
131+
} finally {
132+
DynamicDataSource.useDefault(false);
133+
}
134+
}
135+
136+
@Around(value = "@annotation(dataSource)")
137+
public Object useDefaultDatasource(ProceedingJoinPoint pjp, UseDefaultDataSource dataSource) throws Throwable {
138+
try {
139+
DynamicDataSource.useDefault(dataSource.value());
140+
return pjp.proceed();
141+
} finally {
142+
if (dataSource.value())
143+
DynamicDataSource.useLast();
144+
}
145+
}
146+
}
115147
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.hsweb.web.datasource.dynamic;
2+
3+
import org.hsweb.web.bean.po.datasource.DataSource;
4+
import org.hsweb.web.core.datasource.DynamicDataSource;
5+
6+
import java.lang.annotation.*;
7+
8+
/**
9+
* 通过注解,动态切换数据源。在动态数据源启用时才生效
10+
*
11+
* @author zhouhao
12+
* @since 2.2
13+
*/
14+
@Target({ElementType.TYPE, ElementType.METHOD})
15+
@Retention(RetentionPolicy.RUNTIME)
16+
@Documented
17+
public @interface UseDataSource {
18+
/**
19+
* 数据源ID{@link DataSource#getId()},通过{@link DynamicDataSource}进行数据源切换。
20+
* <br>
21+
* 如果数据源id不存在,将使用默认的数据源
22+
*
23+
* @return 数据源ID
24+
* @see DynamicDataSource#use(String)
25+
*/
26+
String value();
27+
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.hsweb.web.datasource.dynamic;
2+
3+
import org.hsweb.web.core.datasource.DynamicDataSource;
4+
5+
import java.lang.annotation.*;
6+
7+
/**
8+
* 通过注解,切换数据源为默认数据源
9+
*
10+
* @author zhouhao
11+
* @see DynamicDataSource#useDefault(boolean)
12+
* @since 2.2
13+
*/
14+
@Target({ElementType.TYPE, ElementType.METHOD})
15+
@Retention(RetentionPolicy.RUNTIME)
16+
@Documented
17+
public @interface UseDefaultDataSource {
18+
19+
/**
20+
* 方法执行后是否切换到上次使用的数据源
21+
*
22+
* @return 是否自动切换为之前的数据源
23+
*/
24+
boolean value() default true;
25+
}

0 commit comments

Comments
 (0)