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 {

    @Test
    public 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) {
        }
    }
}

运行结果:
这里写图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小工匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值