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.mzph.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…