@Slf4j
@Aspect
@Component
public class DataSourceAspect implements Ordered {
private final static Logger log = LoggerFactory.getLogger(DataSourceAspect.class);
//对com.example.demo.modules包下的所有子包,类,方法生产作用
@Pointcut("@annotation(com.example.demo.datasources.annotation.DataSource) && execution(* com.example.demo.modules..*.*(..))")
public void dataSourcePointCut() {
}
@Around("dataSourcePointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
DataSource ds = method.getAnnotation(DataSource.class);
System.out.println("=================" + ds.name());
try {
Object obj = point.proceed();
System.out.println("================= End");
return obj;
} finally {
log.debug("clean datasource");
}
}
@Override
public int getOrder() {
return 1;
}
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {
String name() default "";
}
@DataSource(name = "ABC")
@Override
public List<Test1> queryList(Map<String, Object> map) {
return test1Dao.queryList(map);
}