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

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无法截获这个方法调用。


  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值