DataSourceAspectAdvice.java
2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.cjs.cms.util.db;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
/**
* 数据源通知
*
* @author tongyufu
*
*/
@Component
@Aspect
public class DataSourceAspectAdvice implements Ordered {
Logger log = LogManager.getLogger();
@Pointcut("execution(* com.cjs.cms.dao..*.*(..))")
public void point() {
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Before("point()")
public void before(JoinPoint joinpoint) {
//默认使用MySql数据源
RoutingDataSource.setDataSourceKey(RoutingDataSource.MYSQL);
Class[] interfaces = joinpoint.getTarget().getClass().getInterfaces();
for (Class clz : interfaces) {
if (clz.isInterface()) {
for (Class clz2 : clz.getInterfaces()) {
if (clz2.isAssignableFrom(OracleDao.class)) {
RoutingDataSource.setDataSourceKey(RoutingDataSource.ORACLE);
break;
}
}
}
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
//@Around("point()")
public Object around(ProceedingJoinPoint joinpoint) throws Throwable {
Class[] interfaces = joinpoint.getTarget().getClass().getInterfaces();
for (Class clz : interfaces) {
if (clz.isInterface()) {
for (Class clz2 : clz.getInterfaces()) {
if (clz2.isAssignableFrom(OracleDao.class)) {
RoutingDataSource.setDataSourceKey(RoutingDataSource.ORACLE);
}
}
}
}
Object response = joinpoint.proceed();
//默认切回MySql数据源
RoutingDataSource.setDataSourceKey(RoutingDataSource.MYSQL);
return response;
}
@Override
public int getOrder() {
return 1;
}
}