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. }

spring的几个通知(前置、后置、环绕、异常、最终)的更多相关文章

  1. Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理

    1.概要 aop:面向方面编程.不改变源代码,还为类增加新的功能.(代理) 切面:实现的交叉功能. 通知:切面的实际实现(通知要做什么,怎么做). 连接点:应用程序执行过程期间,可以插入切面的地点. ...

  2. Spring AOP前置通知和后置通知

    Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或基于XML配置的AOP 在Spring中启用Aspect ...

  3. spring 切面 前置后置通知 环绕通知demo

    环绕通知: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  4. AOP 环绕通知 集成了前置 后置 返回通知等功能

    AOP 环绕通知 集成了前置 后置 返回通知等功能

  5. Spring Bean前置后置处理器的使用

    Spirng中BeanPostProcessor和InstantiationAwareBeanPostProcessorAdapter两个接口都可以实现对bean前置后置处理的效果,那这次先讲解一下B ...

  6. pytest_前置后置

    今天总结下pytest,pytest简直就是python自动化中的高富帅,各种操作,哈哈 这次总结主要涉及到了以下几点: 1.unittest中的setUp.tearDown.setUpClass.t ...

  7. unittest的前置后置,pytest的fixture和共享机制conftest.py

    Unittest setUp/tearDown setUp当中得到的变量,用self.xxx = value传递给测试用例 setUpClass/tearDownClass setupClass当中得 ...

  8. [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  9. Spring初学之xml实现AOP前置通知、后置通知、返回通知、异常通知等

    实现两个整数的加减乘除,在每个方法执行前后打印日志. ArithmeticCalculator.java: package spring.aop.impl.xml; public interface ...

  10. Spring初学之annotation实现AOP前置通知、后置通知、返回通知、异常通知。

    实现两个整数的加减乘除.在执行每个方法之前打印日志. ArithmeticCalculator.java: package spring.aop.impl; public interface Arit ...

随机推荐

  1. sublime text3 插件CTags

    1.打开Sublime Text 2/3软件,在Preferences(设置)菜单中打开Package Control(插件管理器)打开菜单后找到install packages,搜索ctags, 回 ...

  2. filezilla 读取目录失败

    用到FTP,本来一直用主动模式,可以最近老是读取目录失败,425 Can't open data connection 和 读取目录列表失败(搞了好久,一天) 问题解决 这个问题主要是由于使用Pass ...

  3. 在Sql Server中使用证书加密数据

    IF NOT EXISTS () CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'QWE23987zxJKL569&agf1$94467GRkjg5k3 ...

  4. 自动适应label

    CGFloat btnH = 300; NSString *text=@"你在这是NSString的对象方法,一个字符串实例调用该方法时,方法会通过传入的参数返回一个CGRect型数据,这个 ...

  5. cmake window下 sh.exe was found in your PATH, here

    在window下 mingw环境下 用 camke 编译Cpp程序 CMake Error at D:/Program Files/CMake/share/cmake-3.8/Modules/CMak ...

  6. Taylor's theorem

    w https://en.wikipedia.org/wiki/Taylor_series

  7. pycharm中选择python interpreter

    pycharm中选择python interpreter pycharm中有两处地方需要选择python解释器: 一处是调试配置(edit configurations)处,这里选择python解释器 ...

  8. 正向代理、Nginx(反向代理、负载均衡、静态资源服务器)

    淘宝tengine文档(本质就是淘宝版的Nginx) http://tengine.taobao.org/book/index.html

  9. C/C++中浮点数输出格式问题

    在C语言中,浮点数的输出格式有三种:%g, %f, %e 首先要说的是%e是采用科学计数法来显示. %g与后两者有一个重要的差别,就是设置输出精度的时候,(C中默认浮点输出精度是6),%g认为,包括整 ...

  10. datetime时间处理

    基本数据获取: In [38]: import datetime as dt In [39]: on = dt.datetime.now() #获取当前准确时间 In [40]: on Out[40] ...