最佳实践:使用Spring AOP对异常进行统一处理(3)

时间:2025-07-10

有必要使用一个统一的异常处理机制 来进行某些异常处理的统一决策。比如对异常进行统一的日志记录,对散落在各处的某类异常进行统一处理等。

class = "com.grgbanking.platform.core.exception.handler.ExceptionMessageTransfer">

<property name = "noCatchedExceptions">

<set>

<value>com.grgbanking.platform.core.exception.SysException</value> <value>com.grgbanking.platform.core.exception.AppException</value> <value>com.grgbanking.platform.core.exception.PasswordOverdueException</value> <value>ernameNotFoundException</value>

</set>

</property>

</bean>

<aop:config proxy-target-class="true">

<!—配置切入点: 所有service类方法执行时

<aop:pointcut id="exPointcut1" expression="execution(* com.grgbanking..service.*.*(..))"/>

<!—配置记录异常日志切面-->

<aop:aspect ref="exceptionLog" order="3">

<!—配置抛出异常后通知,通知方法是log, ex是log方法中异常参数的名称--> <aop:after-throwing pointcut-ref="exPointcut1" method="log" throwing="ex"/>

</aop:aspect>

<!—配置异常转换切面,通知方法是handle, ex是handle方法中异常参数的名称-->

<aop:aspect ref=" exceptionTransfer " order="1">

<aop:after-throwing pointcut-ref="exPointcut1" method="handle" throwing="ex"/>

</aop:aspect>

</aop:config>

Spring AOP中具体实现拦截处理的类:

/**

* 拦截系统异常记录日志.

*

* @author yrliang

*

*/

public class ExceptionLogger {

private Logger logger = LoggerFactory.getLogger(this.getClass()); private Set<String> noCatchedExceptions = new HashSet<String>();

public Set<String> getNoCatchedExceptions() {

return noCatchedExceptions;

}

最佳实践:使用Spring AOP对异常进行统一处理(3).doc 将本文的Word文档下载到电脑

精彩图片

热门精选

大家正在看

× 游客快捷下载通道(下载后可以自由复制和排版)

限时特价:7 元/份 原价:20元

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219