Spring AOP统一异常处理

1、添加依赖

1         <dependency>  
2             <groupId>org.springframework.boot</groupId>
3             <artifactId>spring-boot-starter-aop</artifactId>
4         </dependency>
5         <dependency>
6             <groupId>cglib</groupId>
7             <artifactId>cglib</artifactId>
8             <version>2.2.2</version>
9         </dependency>

2、在application.properties中添加配置

1 #aop
2 spring.aop.proxy-target-class=true

3、具体代码

 1 package cn.creditcrest.finance.rest.advice;
 2 
 3 import org.aspectj.lang.JoinPoint;
 4 import org.aspectj.lang.annotation.AfterThrowing;
 5 import org.aspectj.lang.annotation.Aspect;
 6 import org.aspectj.lang.annotation.Pointcut;
 7 import org.slf4j.Logger;
 8 import org.slf4j.LoggerFactory;
 9 import org.springframework.stereotype.Component;
10 
11 @Aspect
12 @Component
13 public class GlobalAspect {
14      
15     @Pointcut("execution(public * cn.creditcrest.finance..*.*(..))")  
16     public void pcMethod(){};  
17     
18     @AfterThrowing(pointcut = "pcMethod()",throwing="e")  
19     public void doException(JoinPoint jp,Throwable e){  
20         if(e!=null){  
21             Logger logger = LoggerFactory.getLogger(jp.getSignature().getClass());
22              logger.error(e.getMessage(),e);
23         }  
24     }
25 
26 }

 

posted @ 2017-05-31 10:13  夏嘻嘻嘻嘻嘻  阅读(4851)  评论(0编辑  收藏  举报