New file |
| | |
| | | package com.ltkj.framework.aspectj; |
| | | |
| | | import com.ltkj.common.annotation.DataSource; |
| | | import com.ltkj.common.utils.StringUtils; |
| | | import com.ltkj.db.DataSourceContextHolder; |
| | | import com.ltkj.framework.datasource.DynamicDataSourceContextHolder; |
| | | import org.aspectj.lang.ProceedingJoinPoint; |
| | | import org.aspectj.lang.annotation.Around; |
| | | import org.aspectj.lang.annotation.Aspect; |
| | | import org.aspectj.lang.annotation.Pointcut; |
| | | import org.aspectj.lang.reflect.MethodSignature; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.core.annotation.AnnotationUtils; |
| | | import org.springframework.core.annotation.Order; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * ClassName: AsynAspect <br/> |
| | | * Description: <br/> |
| | | * date: 2025/2/15 18:07<br/> |
| | | * |
| | | * @author zjh<br /> |
| | | */ |
| | | @Aspect |
| | | @Order(2) |
| | | @Component |
| | | public class AsynAspect { |
| | | |
| | | protected Logger logger = LoggerFactory.getLogger(getClass()); |
| | | |
| | | @Pointcut("@annotation(org.springframework.scheduling.annotation.Async)" |
| | | + "|| @within(org.springframework.scheduling.annotation.Async)") |
| | | public void dsPointCut() { |
| | | |
| | | } |
| | | |
| | | @Around("dsPointCut()") |
| | | public Object around(ProceedingJoinPoint point) throws Throwable { |
| | | String key = DataSourceContextHolder.getDataSourceKey(); |
| | | logger.info("执行之前"); |
| | | logger.info("DataSourceContextHolder ->{}",key); |
| | | logger.info("DynamicDataSourceContextHolder ->{}", DynamicDataSourceContextHolder.getDataSourceType()); |
| | | |
| | | DynamicDataSourceContextHolder.setDataSourceType(key); |
| | | |
| | | try { |
| | | return point.proceed(); |
| | | } finally { |
| | | logger.info("执行之后"); |
| | | logger.info("DataSourceContextHolder ->{}",key); |
| | | logger.info("DynamicDataSourceContextHolder ->{}",DynamicDataSourceContextHolder.getDataSourceType()); |
| | | // 销毁数据源 在执行方法之后 |
| | | DynamicDataSourceContextHolder.clearDataSourceType(); |
| | | logger.info("执行之后 clear之后"); |
| | | logger.info("DataSourceContextHolder ->{}",key); |
| | | logger.info("DynamicDataSourceContextHolder ->{}",DynamicDataSourceContextHolder.getDataSourceType()); |
| | | } |
| | | } |
| | | } |