CZT
2023-11-27 c206acfaedc69c390fb67daa81bc686f58a212ef
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
package com.ld.igds.aop;
 
import com.ld.igds.annotation.DataSource;
import com.ld.igds.conf.DataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
 
/**
 * 在使用不同数据源时候,使用@DatSource注解
 * @author Andy
 *
 */
@Slf4j
@Component
@Aspect
@Order(-100)
public class DataSourceAspect {
 
    @Pointcut("@within(com.ld.igds.annotation.DataSource) || "
            + "@annotation(com.ld.igds.annotation.DataSource)")
    public void pointCut() {
 
    }
 
    @Before("pointCut() && @annotation(dataSource)")
    public void doBefore(DataSource dataSource) {
        log.debug("当前选择的数据源为:{}",dataSource.value().getValue());
        DataSourceContextHolder.setDataSource(dataSource.value().getValue());
    }
 
    @After("pointCut()")
    public void doAfter() {
        DataSourceContextHolder.clear();
    }
}