欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

Spring-AOP 通过配置文件实现 异常抛出增强

发布时间:2025/3/21 javascript 38 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Spring-AOP 通过配置文件实现 异常抛出增强 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

  • 概述
  • 实例

概述

异常抛出增强表示在目标方法抛出异常后实施增强,最适合的场景是事务管理,比如当参与事事务的方法抛出异常后需要回滚事务。

异常抛出增强类需要实现ThrowsAdvice接口,ThrowsAdvice接口并没有定义任何的方法,它只是一个标志接口。

在运行期,Spring采用反射的机制来进行判断。

我们必须采用以下的形式来定义异常抛出的方法

public void afterThrowing(Method method,Object[] args,Object target,Throwable t)
  • 方法名必须为afterThrowing

  • 方法入参中前三个入参是可选的,即要么同时存在,要么都没有

  • 最后一个入参是Throwable及其子类,必须得有。

  • 也可以在异常增强类中定义多个方法,Spring会自动选择匹配的方法来进行调用。 在类的继承树上,两个类的距离越近,则两个类的相似度越高,那么当方法抛出异常时,会优先选取异常入参和抛出的异常相似度最高的afterThrowing方法。

实例

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster

我们创建示例来演示一下,步骤如下:
创建业务实现类:ForumService.java
创建业务增强类:TransactionManager.java
创建配置文件:conf-advice.xml
创建增强测试类:ThrowsAdviceTest.java

首先,创建业务逻辑类ForumService

package com.xgj.aop.spring.advice.throwsAdvice;public class ForumService {public void removeForum() {// 进行相应的数据库操作,但这里只为演示抛出异常throw new RuntimeException("removeForum:Exception...");}public void updateForum() {// 进行相应的数据库操作,但这里只为演示抛出异常throw new RuntimeException("updateForum:Exception...");} }

接下来我们创建增强类TransactionManager

package com.xgj.aop.spring.advice.throwsAdvice;import java.lang.reflect.Method;import org.springframework.aop.ThrowsAdvice;public class TransactionManager implements ThrowsAdvice {/*** 捕获异常并打印异常名称* * @param method* 目标对象对应方法* @param args* 方法入参* @param target* 目标对象* @param ex* 运行方法所捕获的异常* @throws Throwable*/public void afterThrowing(Method method, Object[] args, Object target,Exception ex) throws Throwable {System.out.println("method:" + method.getName());System.out.println("抛出异常:" + ex.getMessage());System.out.println("成功回滚事务");} }

接下来我们编写对应的配置文件

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:p="http://www.springframework.org/schema/p"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="forumServiceTarget" class="com.xgj.aop.spring.advice.throwsAdvice.ForumService"/><bean id="transactionManager" class="com.xgj.aop.spring.advice.throwsAdvice.TransactionManager"/><bean id="forumService" class="org.springframework.aop.framework.ProxyFactoryBean"p:proxyTargetClass="true"p:target-ref="forumServiceTarget"p:interceptorNames="transactionManager"/></beans>

创建相应的测试类进行测试

package com.xgj.aop.spring.advice.throwsAdvice;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class ThrowsAdviceTest {@Testpublic void test() {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/aop/spring/advice/throwsAdvice/conf-advice.xml");ForumService forumService = ctx.getBean("forumService",ForumService.class);try {forumService.removeForum();} catch (Exception e) {}try {forumService.updateForum();} catch (Exception e) {}} }

运行结果:

《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读

总结

以上是生活随笔为你收集整理的Spring-AOP 通过配置文件实现 异常抛出增强的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。