spring的几个通知(前置、后置、环绕、异常、最终)
1、没有异常的

2、有异常的

1、被代理类接口Person.java
package com.xiaostudy; /**
* @desc 被代理类接口
*
* @author xiaostudy
*
*/
public interface Person { public void add();
public void update();
public void delete();
}
2、被代理类PersonImple.java
package com.xiaostudy; /**
* @desc 被代理类
*
* @author xiaostudy
*
*/
public class PersonImple implements Person { /**
* @desc 实现接口方法
*/
public void add() {
System.out.println("add()>>>>>>>>");
} @Override
public void update() {
System.out.println("update()>>>>>>>>");
// int i = 1/0;
} @Override
public void delete() {
System.out.println("delete()>>>>>>>>");
} }
3、MyAspectJ.java
package com.xiaostudy; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; /**
* @desc 通知类
*
* @author xiaostudy
*
*/
public class MyAspectJ { public void myBefort(JoinPoint joinPoint) {
System.out.println("前置通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName());
} public void myAfterReturning(JoinPoint joinPoint, Object ret) {
System.out.println("后置通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName()
+ ", ret: " + ret);
} public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕通知====前>>>>>>>>>>>");
Object obj = joinPoint.proceed();
System.out.println("环绕通知====后<<<<<<<<<<<");
return obj;
} public void myThrowint(JoinPoint joinPoint, Throwable e) {
System.out.println("异常通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName()
+ ", e: " + e.getMessage());
System.exit(0);
} public void myAfter(JoinPoint joinPoint) {
System.out.println("最终通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName());
}
}
4、spring的配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 创建被代理类 -->
<bean id="person" class="com.xiaostudy.PersonImple"></bean>
<!-- 创建切面类 -->
<bean id="advice" class="com.xiaostudy.MyAspectJ"></bean>
<!-- springAOP编程 -->
<aop:config>
<!-- 将切面类 声明“切面”,从而获得通知(方法) -->
<aop:aspect ref="advice">
<!-- 声明一个切入点,所有的通知都可以使用 -->
<aop:pointcut expression="execution(* com.xiaostudy.PersonImple.*(..))" id="myPointcut"/>
<!-- 前置通知: method表示:方法名,pointcut-ref表示:所有的通知共享,(pointcut表示:只有当前通知可用,其他的不能用) -->
<aop:before method="myBefort" pointcut-ref="myPointcut"/>
<!-- 后置通知:returning表示:后置通知的第二个参数名,内容是方法的返回值 -->
<aop:after-returning method="myAfterReturning" returning="ret" pointcut-ref="myPointcut"/>
<!-- 环绕通知 -->
<aop:around method="myAround" pointcut-ref="myPointcut"/>
<!-- 异常通知:throwing表示:异常通知的第二个参数,内容是异常信息 -->
<aop:after-throwing method="myThrowint" throwing="e" pointcut-ref="myPointcut"/>
<!-- 最终通知 -->
<aop:after method="myAfter" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
</beans>
5、测试类Test.java
package com.xiaostudy; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @desc 测试类
*
* @author xiaostudy
*
*/
public class Test { public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = ac.getBean("person", Person.class);
person.add();
person.update();
person.delete();
} }
spring的几个通知(前置、后置、环绕、异常、最终)的更多相关文章
- Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理
1.概要 aop:面向方面编程.不改变源代码,还为类增加新的功能.(代理) 切面:实现的交叉功能. 通知:切面的实际实现(通知要做什么,怎么做). 连接点:应用程序执行过程期间,可以插入切面的地点. ...
- Spring AOP前置通知和后置通知
Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或基于XML配置的AOP 在Spring中启用Aspect ...
- spring 切面 前置后置通知 环绕通知demo
环绕通知: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...
- AOP 环绕通知 集成了前置 后置 返回通知等功能
AOP 环绕通知 集成了前置 后置 返回通知等功能
- Spring Bean前置后置处理器的使用
Spirng中BeanPostProcessor和InstantiationAwareBeanPostProcessorAdapter两个接口都可以实现对bean前置后置处理的效果,那这次先讲解一下B ...
- pytest_前置后置
今天总结下pytest,pytest简直就是python自动化中的高富帅,各种操作,哈哈 这次总结主要涉及到了以下几点: 1.unittest中的setUp.tearDown.setUpClass.t ...
- unittest的前置后置,pytest的fixture和共享机制conftest.py
Unittest setUp/tearDown setUp当中得到的变量,用self.xxx = value传递给测试用例 setUpClass/tearDownClass setupClass当中得 ...
- [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring初学之xml实现AOP前置通知、后置通知、返回通知、异常通知等
实现两个整数的加减乘除,在每个方法执行前后打印日志. ArithmeticCalculator.java: package spring.aop.impl.xml; public interface ...
- Spring初学之annotation实现AOP前置通知、后置通知、返回通知、异常通知。
实现两个整数的加减乘除.在执行每个方法之前打印日志. ArithmeticCalculator.java: package spring.aop.impl; public interface Arit ...
随机推荐
- springMVC的url-pattern /和/*的区别
总之,关于web.xml的url映射的小知识: <url-pattern>/</url-pattern> 会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样 ...
- mysql返回字符串在另外一个字符串中第n次出现的方法。
SELECT SUBSTRING_INDEX("迟到50分钟,早退15分钟","分钟",2); 返回:迟到50分钟,早退15
- phpstorm 9.0最新 注册码
phpstorm注册码: User Name:newasp 01.License Key: ===== LICENSE BEGIN ===== 14617-12042010 00001xrVkhnPu ...
- 巨蟒python全栈开发数据库前端6:事件onclick的两种绑定方式&&onblur和onfocus事件&&window.onload解释&&小米商城讲解
1.回顾上节内容(JavaScript) 一.JavaScript概述 1.ECMAScript和JavaScript的关系 2.ECMAScript的历史 3.JavaScript是一门前后端都可以 ...
- mysql 获取id最大值
数据库表中id列不为自动增加,需要程序来增加id的SQL SELECTCASE IFNULL(MAX(id),1)WHEN 1 THEN 1ELSE MAX(id) + 1END AS newmaxi ...
- CListCtrl消息及解释
对于CListCtrl消息的解释:[来自网络]LVN_BEGINDRAG 鼠标左键正在被触发以便进行拖放操作(当鼠标左键开始拖拽列表视图控件中的项目时产生) LVN_BEGINRDRAG 鼠标右键正在 ...
- for...in循环取Json数据
var result = { "Tables":{ "B2B_DS_ORDERMX0":{ "ordernum":"tables- ...
- 从库函数解析STM32地址映射
STM32的存储映射是靠基地址和地址偏移实现的. 32位的M3有4GB的寻址空间,其中用于片上外设的有512MB,基地址为0x40000000. M3各外设基地址,包括片上外设.片上静态RAM和FLA ...
- hdu 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- Keras网络层之常用层Core
常用层 常用层对应于core模块,core内部定义了一系列常用的网络层,包括全连接.激活层等 Dense层 keras.layers.core.Dense(units, activation=None ...