javascript
Spring-AOP 通过配置文件实现 异常抛出增强
- 概述
- 實(shí)例
概述
異常拋出增強(qiáng)表示在目標(biāo)方法拋出異常后實(shí)施增強(qiáng),最適合的場(chǎng)景是事務(wù)管理,比如當(dāng)參與事事務(wù)的方法拋出異常后需要回滾事務(wù)。
異常拋出增強(qiáng)類需要實(shí)現(xiàn)ThrowsAdvice接口,ThrowsAdvice接口并沒有定義任何的方法,它只是一個(gè)標(biāo)志接口。
在運(yùn)行期,Spring采用反射的機(jī)制來進(jìn)行判斷。
我們必須采用以下的形式來定義異常拋出的方法
public void afterThrowing(Method method,Object[] args,Object target,Throwable t)方法名必須為afterThrowing
方法入?yún)⒅星叭齻€(gè)入?yún)⑹强蛇x的,即要么同時(shí)存在,要么都沒有
最后一個(gè)入?yún)⑹荰hrowable及其子類,必須得有。
也可以在異常增強(qiáng)類中定義多個(gè)方法,Spring會(huì)自動(dòng)選擇匹配的方法來進(jìn)行調(diào)用。 在類的繼承樹上,兩個(gè)類的距離越近,則兩個(gè)類的相似度越高,那么當(dāng)方法拋出異常時(shí),會(huì)優(yōu)先選取異常入?yún)⒑蛼伋龅漠惓O嗨贫茸罡叩腶fterThrowing方法。
實(shí)例
代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster
我們創(chuàng)建示例來演示一下,步驟如下:
創(chuàng)建業(yè)務(wù)實(shí)現(xiàn)類:ForumService.java
創(chuàng)建業(yè)務(wù)增強(qiáng)類:TransactionManager.java
創(chuàng)建配置文件:conf-advice.xml
創(chuàng)建增強(qiáng)測(cè)試類:ThrowsAdviceTest.java
首先,創(chuàng)建業(yè)務(wù)邏輯類ForumService
package com.xgj.aop.spring.advice.throwsAdvice;public class ForumService {public void removeForum() {// 進(jìn)行相應(yīng)的數(shù)據(jù)庫操作,但這里只為演示拋出異常throw new RuntimeException("removeForum:Exception...");}public void updateForum() {// 進(jìn)行相應(yīng)的數(shù)據(jù)庫操作,但這里只為演示拋出異常throw new RuntimeException("updateForum:Exception...");} }接下來我們創(chuàng)建增強(qiáng)類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* 目標(biāo)對(duì)象對(duì)應(yīng)方法* @param args* 方法入?yún)? @param target* 目標(biāo)對(duì)象* @param ex* 運(yùn)行方法所捕獲的異常* @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("成功回滾事務(wù)");} }接下來我們編寫對(duì)應(yīng)的配置文件
<?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>創(chuàng)建相應(yīng)的測(cè)試類進(jìn)行測(cè)試
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) {}} }運(yùn)行結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的Spring-AOP 通过配置文件实现 异常抛出增强的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring-AOP 通过配置文件实现
- 下一篇: Spring-AOP 通过配置文件实现