DataSourceSwitchAspect.java 1.02 KB
Newer Older
liqin's avatar
liqin committed
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
package cn.wisenergy.chnmuseum.party.common.multidatasource;

import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *  这是为了保证AOP在事务注解之前生效,Order的值越小,优先级越高
 */
//@Order(-100)
//@Component
//@Aspect
public class DataSourceSwitchAspect {

    private static final Logger LOGGER = LoggerFactory.getLogger(DataSourceSwitchAspect.class);

    @Pointcut("execution(* cn.gov.zunyi.business.hall.mapper.db1..*.*(..))")
    private void db1Aspect() {
    }

    @Pointcut("execution(* cn.gov.zunyi.business.hall.mapper.db2..*.*(..))")
    private void db2Aspect() {
    }

    @Before("db1Aspect()")
    public void db1() {
        LOGGER.info("切换到db1 数据源...");
        DbContextHolder.setDbType(DBTypeEnum.datasource1);
    }

    @Before("db2Aspect()")
    public void db2 () {
        LOGGER.info("切换到db2 数据源...");
        DbContextHolder.setDbType(DBTypeEnum.datasource2);
    }

}