日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring23:Aspectj实现异常通知@AfterThrowing

發布時間:2025/6/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring23:Aspectj实现异常通知@AfterThrowing 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

切入類:

?

package com.atChina.Test4;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before;/*** @Aspect:來自aspectj框架,表示當前類是切面類* 切面類是用來給業務方法增強功能的類*/ @Aspect public class MyAspect {/*** @AfterThrowing:異常通知,目標方法拋出異常時執行* 屬性:1. value,表示切入點表達式(切面功能加入的位置)* 2. throwing,自定義的變量,表示目標方法的異常對象,需要和通知方法的參數名* 位置:在方法的上面* * 特點:* 1.在目標方法拋出異常時執行* 2.不是異常處理程序,只是得到異常的消息* 3.可以作為目標方法的監控程序,檢查目標方法是否正常執行*/@AfterThrowing(value="execution(* *..SomeServiceImpl.doException())", throwing="ex")public void myAfterThrowing(Throwable ex){System.out.println("出現了異常"+ex);} }

?普通bean

package com.atChina.Test4;public interface SomeService {public void doSome();public String doOther(String params);public Student getStudent();public String doAround();public void doException(); }package com.atChina.Test4;public class SomeServiceImpl implements SomeService {@Overridepublic void doSome() {System.out.println("執行了doSome業務方法...");}@Overridepublic String doOther(String params) {// TODO Auto-generated method stubSystem.out.println("執行了doSome業務方法..."+params);return params;}@Overridepublic Student getStudent() {Student st = new Student();st.setAge(22);st.setName("孫悟空");return st;}@Overridepublic String doAround() {System.out.println("執行了doAround業務方法...");return "doAround";}@Overridepublic void doException() {// TODO Auto-generated method stubint i = 5/0;System.out.println("doException...");} }package com.atChina.Test4;public class Student {private String name;private int age;public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + "]";} }

?配置文件

<?xml version="1.0" encoding="UTF-8"?> <!-- 引用Spring的多個Schema空間的格式定義文件 --> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd "><!-- 聲明目標類對象 --><bean id="someService" class="com.atChina.Test4.SomeServiceImpl" /><!-- 聲明切面類對象 --><bean id="myAspect" class="com.atChina.Test4.MyAspect" /><!-- 聲明自動代理生成器,創建代理對象 --><aop:aspectj-autoproxy /> <!-- 尋找aspectj框架能夠識別的標簽 --> </beans>

測試方法:

@Testpublic void test2(){String configLocation = "com/atChina/Test4/applicationContext.xml"; // 類路徑的根目錄ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);// 目標對象有接口,aspectj默認使用的是jdk動態代理SomeService proxy = (SomeService) ctx.getBean("someService");System.out.println(proxy.getClass().getName());// proxy.doSome();System.out.println("===================================");proxy.doException();}

?

總結

以上是生活随笔為你收集整理的spring23:Aspectj实现异常通知@AfterThrowing的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。