spring23:Aspectj实现异常通知@AfterThrowing
生活随笔
收集整理的這篇文章主要介紹了
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 算法三:无重复字符的最长子串
- 下一篇: spring25:@Pointcut管理