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 }

?

轉載于:https://www.cnblogs.com/xiaostudy/p/9535857.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388744.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388744.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388744.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

每個Power BI開發人員的Power Query提示

If someone asks you to define the Power Query, what should you say? If you’ve ever worked with Power BI, there is no chance that you haven’t used Power Query, even if you weren’t aware of it. Therefore, one could easily say that Power Query is the “he…

c# PDF 轉換成圖片

1.新建項目 2.新增一個新文件夾“lib”&#xff08;主要是為了存放引用的dll&#xff09; 3.將“gsdll32.dll 、PDFLibNet.dll 、PDFView.dll”3個dll添加到文件夾中 4.項目添加“PDFLibNet.dll 、PDFView.dll”2個類庫的引用&#xff0c;并將gsdll32.dll 拷貝到項目生產根…

java finally在return_Java finally語句到底是在return之前還是之后執行?

點擊上方“方志朋”&#xff0c;選擇“置頂或者星標”你的關注意義重大&#xff01;網上有很多人探討Java中異常捕獲機制try...catch...finally塊中的finally語句是不是一定會被執行&#xff1f;很多人都說不是&#xff0c;當然他們的回答是正確的&#xff0c;經過我試驗&#…

oracle 死鎖

為什么80%的碼農都做不了架構師&#xff1f;>>> ORA-01013: user requested cancel of current operation 轉載于:https://my.oschina.net/8808/blog/2994537

面試題:二叉樹的深度

題目描述&#xff1a;輸入一棵二叉樹&#xff0c;求該樹的深度。從根結點到葉結點依次經過的結點&#xff08;含根、葉結點&#xff09;形成樹的一條路徑&#xff0c;最長路徑的長度為樹的深度。 思路&#xff1a;遞歸 //遞歸 public class Solution {public int TreeDepth(Tre…

a/b測試_如何進行A / B測試?

a/b測試The idea of A/B testing is to present different content to different variants (user groups), gather their reactions and user behaviour and use the results to build product or marketing strategies in the future.A / B測試的想法是將不同的內容呈現給不同…

hibernate h2變mysql_struts2-hibernate-mysql開發案例 -解道Jdon

Hibernate專題struts2-hibernate-mysql開發案例與源碼源碼下載本案例展示使用Struts2&#xff0c;Hibernate和MySQL數據庫開發一個個人音樂管理器Web應用程序。&#xff0c;可將您的音樂收藏添加到數據庫中。功能有&#xff1a;顯示一個添加記錄的表單和所有的音樂收藏的列表。…

P5024 保衛王國

傳送門 我現在還是不明白為什么NOIPd2t3會是一道動態dp…… 首先關于動態dp可以看這里 然后這里就是把把矩陣給改一改&#xff0c;改成這個形式\[\left[dp_{i-1,0},dp_{i-1,1}\right]\times \left[\begin{matrix}\infty&ldp_{i,1}\\ldp_{i,0}&ldp_{i,1}\end{matrix}\ri…

提取圖像感興趣區域_從圖像中提取感興趣區域

提取圖像感興趣區域Welcome to the second post in this series where we talk about extracting regions of interest (ROI) from images using OpenCV and Python.歡迎來到本系列的第二篇文章&#xff0c;我們討論使用OpenCV和Python從圖像中提取感興趣區域(ROI)。 As a rec…

解決java compiler level does not match the version of the installed java project facet

ava compiler level does not match the version of the installed java project facet錯誤的解決 因工作的關系&#xff0c;Eclipse開發的Java項目拷來拷去&#xff0c;有時候會報一個很奇怪的錯誤。明明源碼一模一樣&#xff0c;為什么項目復制到另一臺機器上&#xff0c;就會…

php模板如何使用,ThinkPHP如何使用模板

到目前為止&#xff0c;我們只是使用了控制器和模型&#xff0c;還沒有接觸視圖&#xff0c;下面來給上面的應用添加視圖模板。首先我們修改下 Action 的 index 操作方法&#xff0c;添加模板賦值和渲染模板操作。PHP代碼classIndexActionextendsAction{publicfunctionindex(){…

理解Windows窗體和WPF中的跨線程調用

你曾開發過Windows窗體程序&#xff0c;可能會注意到有時事件處理程序將拋出InvalidOperationException異常&#xff0c;信息為“ 跨線程調用非法&#xff1a;在非創建控件的線程上訪問該控件”。這種Windows窗體應用程序中 跨線程調用時的一個最為奇怪的行為就是&#xff0c;有…

什么是嵌入式系統

在我們的日常生活中&#xff0c;我們經常使用許多使用嵌入式系統技術設計的電氣和電子電路和套件。計算機&#xff0c;手機&#xff0c;平板&#xff0c;筆記本電腦&#xff0c;數字電子系統以及其他電子和電子設備都是使用嵌入式系統設計的。 什么是嵌入式系統&#xff1f;將硬…

面向數據科學家的實用統計學_數據科學家必知的統計數據

面向數據科學家的實用統計學Beginners usually ignore most foundational statistical knowledge. To understand different models, and various techniques better, these concepts are essential. These work as baseline knowledge for various concepts involved in data …

字符串、指針、引用、數組基礎

1.字符串&#xff1a;字符是由單引號所括住的單個字母、數字或符號。若將單引號改為雙引號&#xff0c;該字符就會變成字符串。它們之間主要的差別是&#xff1a;雙引號的字符串“A”會比單引號的字符串’A’在字符串的最后補上一個結束符’\0’&#xff08;Null字符&#xff0…

suse安裝php,SUSE下安裝LAMP

安裝Apache可以看到編譯安裝Apache出錯&#xff0c;rpm包安裝gcc (首先要安裝GCC)makemake install修改apache端口cd /home/sxit/apache2vi conf/httpd.confListen 8000啟動 apache/home/root/apache2/bin/apachectl start(stop restart)http://localhost:8000安裝一下PHP開發…

自己動手寫事件總線(EventBus)

2019獨角獸企業重金招聘Python工程師標準>>> 本文由云社區發表 事件總線核心邏輯的實現。 <!--more--> EventBus的作用 Android中存在各種通信場景&#xff0c;如Activity之間的跳轉&#xff0c;Activity與Fragment以及其他組件之間的交互&#xff0c;以及在某…

viz::viz3d報錯_我可以在Excel中獲得該Viz嗎?

viz::viz3d報錯Have you ever found yourself in the following situation?您是否遇到以下情況&#xff1f; Your team has been preparing and working tireless hours to create and showcase the end product — an interactive visual dashboard. It’s a culmination of…

php 數組合并字符,PHP將字符串或數組合并到一個數組內方法

本文主要和大家分享PHP將字符串或數組合并到一個數組內方法&#xff0c;有兩種方法&#xff0c;希望希望能幫助到大家。一般寫法&#xff1a;<?php /*** add a string or an array to another array** param array|string $val* param array $array*/function add_val_to_a…

xcode 4 最低的要求是 10.6.6的版本,如果你是 10.6.3的版本,又不想升級的話。可以考慮通過修改版本號的方法進行安裝

xcode 4 最低的要求是 10.6.6的版本&#xff0c;如果你是 10.6.3的版本&#xff0c;又不想升級的話。可以考慮通過修改版本號的方法進行安裝。 一、打開控制臺&#xff1b; 二、使用root用戶&#xff1b; 命令&#xff1a;sudo -s 之后輸入密碼即可 三、編輯 /System/Library/C…