spring的幾個通知(前置、后置、環繞、異常、最終)


1、沒有異常的

2、有異常的


1、被代理類接口Person.java

 1 package com.xiaostudy;
 2 
 3 /**
 4  * @desc 被代理類接口
 5  * 
 6  * @author xiaostudy
 7  *
 8  */
 9 public interface Person {
10 
11     public void add();
12     public void update();
13     public void delete();
14 }

2、被代理類PersonImple.java

 1 package com.xiaostudy;
 2 
 3 /**
 4  * @desc 被代理類
 5  * 
 6  * @author xiaostudy
 7  *
 8  */
 9 public class PersonImple implements Person {
10 
11     /**
12      * @desc 實現接口方法
13      */
14     public void add() {
15         System.out.println("add()>>>>>>>>");
16     }
17 
18     @Override
19     public void update() {
20         System.out.println("update()>>>>>>>>");
21 //        int i = 1/0;
22     }
23 
24     @Override
25     public void delete() {
26         System.out.println("delete()>>>>>>>>");
27     }
28     
29 }

3、MyAspectJ.java

 1 package com.xiaostudy;
 2 
 3 import org.aspectj.lang.JoinPoint;
 4 import org.aspectj.lang.ProceedingJoinPoint;
 5 
 6 /**
 7  * @desc 通知類
 8  * 
 9  * @author xiaostudy
10  *
11  */
12 public class MyAspectJ {
13     
14     public void myBefort(JoinPoint joinPoint) {
15         System.out.println("前置通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName());
16     }
17     
18     public void myAfterReturning(JoinPoint joinPoint, Object ret) {
19         System.out.println("后置通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName()
20                 + ", ret: " + ret);
21     }
22     
23     public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
24         System.out.println("環繞通知====前>>>>>>>>>>>");
25         Object obj = joinPoint.proceed();
26         System.out.println("環繞通知====后<<<<<<<<<<<");
27         return obj;
28     }
29     
30     public void myThrowint(JoinPoint joinPoint, Throwable e) {
31         System.out.println("異常通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName()
32                 + ", e: " + e.getMessage());
33         System.exit(0);
34     }
35     
36     public void myAfter(JoinPoint joinPoint) {
37         System.out.println("最終通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName());
38     }
39 }

4、spring的配置文件applicationContext.xml

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6                               http://www.springframework.org/schema/beans/spring-beans.xsd
 7                               http://www.springframework.org/schema/aop 
 8                               http://www.springframework.org/schema/aop/spring-aop.xsd">
 9     <!-- 創建被代理類 -->
10     <bean id="person" class="com.xiaostudy.PersonImple"></bean>
11     <!-- 創建切面類 -->
12     <bean id="advice" class="com.xiaostudy.MyAspectJ"></bean>
13     <!-- springAOP編程 -->
14     <aop:config>
15         <!-- 將切面類 聲明“切面”,從而獲得通知(方法) -->
16         <aop:aspect ref="advice">
17             <!-- 聲明一個切入點,所有的通知都可以使用 -->
18             <aop:pointcut expression="execution(* com.xiaostudy.PersonImple.*(..))" id="myPointcut"/>
19             <!-- 前置通知: method表示:方法名,pointcut-ref表示:所有的通知共享,(pointcut表示:只有當前通知可用,其他的不能用) -->
20             <aop:before method="myBefort" pointcut-ref="myPointcut"/>
21             <!-- 后置通知:returning表示:后置通知的第二個參數名,內容是方法的返回值 -->
22             <aop:after-returning method="myAfterReturning" returning="ret" pointcut-ref="myPointcut"/>
23             <!-- 環繞通知 -->
24             <aop:around method="myAround" pointcut-ref="myPointcut"/>
25             <!-- 異常通知:throwing表示:異常通知的第二個參數,內容是異常信息 -->
26             <aop:after-throwing method="myThrowint" throwing="e" pointcut-ref="myPointcut"/>
27             <!-- 最終通知 -->
28             <aop:after method="myAfter" pointcut-ref="myPointcut"/>
29         </aop:aspect>
30     </aop:config>
31 </beans>

 

5、測試類Test.java

 1 package com.xiaostudy;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 /**
 7  * @desc 測試類
 8  * 
 9  * @author xiaostudy
10  *
11  */
12 public class Test {
13 
14     public static void main(String[] args) {
15         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
16         Person person = ac.getBean("person", Person.class);
17         person.add();
18         person.update();
19         person.delete();
20     }
21 
22 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM