Spring Boot使用AOP在指定方法执行完后执行异步处理

岑毅庵
2023-12-01
1.在pom.xml中加入如下依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>

</dependency>

2.在Spring Boot的application启动类上加上
      @EnableAsync(proxyTargetClass = true)
       proxyTargetClass为可选配置相,默认为false

3.创建异步执行类MyAsyncTask

@Component
public class MyAsyncTask {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Async
    public void refreshMyDbAsync(String url, String id) {
    	for(int i=0; i < 10; i++){
    	    logger.info("2.In Async Method id:" + id + " count:" + i + " URL:" + url);
    	    try {
		    Thread.sleep(5000);
	    } catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
    	}
    }
}
4.创建Aspect类,定义切入点并在切入点执行后调用异类执行类的异步方法
@Aspect
@Component
public class MyAspect {

    private Logger logger = LoggerFactory.getLogger(getClass());
	
    @Autowired
    private MyAsyncTask myAsyncTask;

    @Pointcut("execution(* com.lantian.controller.Controller1.detail(..)) || "
            + "execution(* com.lantian.controller.Controller1.list(..)) || "
            + "execution(* com.lantian.controller.Controller2.detail(..))")
    public void modifyMethod() {}

    @AfterReturning(returning = "ret", pointcut = "modifyMethod()")
    public void afterModify(Object ret) {
    	//获取request
    	HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    	String url = request.getRequestURL().toString();
    	String id = request.getParameter("id");
    	logger.info("1.Before Async Method URL:"+url);
    	logger.info(ret+"");
    	myAsyncTask.refreshMyDbAsync(url, id);
    	logger.info("3.After Async Method URL:"+url);
    }
}

5.注意事项:
   异步方法不能直接写在Aspect类里,否则不会异步执行。
      spring对@Transactional注解时也有类似问题,spring扫描时具有@Transactional注解方法的类时,是生成一个代理类,由代理类去开启关闭事务,而在同一个类中,方法调用是在类体内执行的,spring无法截获这个方法调用。


 类似资料: