前言
今天使用 aop 完成日志记录的时候,before,after,afterreturing都是正常执行完成的。对异常也要进行记录,但是数据存不进去,百度了下是因为数据库的事务也是基于aop,在代码中之前开启事务,代码之后提交事务,但是因为日志的aop在事务的事务之后执行,所以需要设置日志的aop的顺序
顺序实现
使用Order接口
@Override
public int value() {
return 1;
}
使用注解的方式
@Order(1)
数值越小,优先级越高
spring实现aop
AOP的顺序
around->before->after->around
->成功 afterreturing
->异常 afterthrowing
@Aspect
@Component
@Order(1)
public class LoanCallbackAspect {
/**
* aop 执行顺序 before after returning
*/
static Map<Long,Long> log_aop_containter =new ConcurrentHashMap();
static Logger logger= LoggerFactory.getLogger(LoanCallbackAspect.class);
static ExecutorService outputMsgUpdateTasks=Executors.newFixedThreadPool(50);
@Autowired
private IService iLogService;
private final static String excutionstr="execution( * com.balabala.loanserviceloansupermarket.controller.LoanCallbackController.*(..) )";
@Pointcut(excutionstr)
private void pointCut(){
}
@Before("pointCut()")
public void doBefore(JoinPoint joinPoint){
}
@AfterReturning(returning="obj", pointcut = "pointCut()")
public void doAfterReturnig(Object obj){
handleOutputMessage(obj,1);
}
@AfterThrowing(value = "pointCut()",throwing = "e")
public void afterThrowing(JoinPoint joinPoint,Exception e) {
handleOutputMessage(e,0);
}
@After(value = "pointCut()")
public void after(JoinPoint joinPoint) {
}
public void handleOutputMessage(Object obj,int success){
}
public Map<String,String> extractRequest(HttpServletRequest request ){
}
}