java写了个接口并写了个实现类:

  1. package myspring.calculator;
  2. public interface IArithmeticCalculator {
  3. public double add(double a, double b);
  4. public double sub(double a, double b);
  5. public double mul(double a, double b);
  6. public double div(double a, double b);
  7. }
  1. package myspring.calculator;
  2. import org.apache.commons.logging.Log;
  3. import org.apache.commons.logging.LogFactory;
  4. public class ArithmeticCalculatorImp implements IArithmeticCalculator {
  5. public double add(double a, double b) {
  6. double result = a + b;
  7. System.out.println(a + " + " + b + " = " + result);
  8. return result;
  9. }
  10. public double sub(double a, double b) {
  11. double result = a - b;
  12. System.out.println(a + " - " + b + " = " + result);
  13. return result;
  14. }
  15. public double mul(double a, double b) {
  16. double result = a * b;
  17. System.out.println(a + " * " + b + " = " + result);
  18. return result;
  19. }
  20. public double div(double a, double b) {
  21. if (b == 0) {
  22. throw new IllegalArgumentException("Division by zero");
  23. }
  24. double result = a / b;
  25. System.out.println(a + " / " + b + " = " + result);
  26. return result;
  27. }
  28. }

在spring配置文件aop-base.xml中配置好

  1. <bean id="arithmeticCalculator" class="myspring.calculator.ArithmeticCalculatorImp" />
  2. <bean id="arithmeticCalculatorProxy"
  3. class="org.springframework.aop.framework.ProxyFactoryBean">
  4. <property name="target" ref="arithmeticCalculator"></property>
  5. <property name="interceptorNames"><!--定义使用何种方式拦截-->
  6. <list>
  7. <!-- <value>logBeforeAdvice</value>
  8. <value>logAfterReturning</value>
  9. <value>logThrowsAdvice</value> -->
  10. <value>logAroundAdvice</value>
  11. </list>
  12. </property>
  13. </bean>

在用到接口里方法的地方加载配置文件并获得代理类:

  1. ApplicationContext ac = new ClassPathXmlApplicationContext("aop-base.xml");
  2. IArithmeticCalculator calculatorProxy =
  3. (IArithmeticCalculator)ac.getBean("arithmeticCalculatorProxy");
  4. calculatorProxy.add(2, 3);
  5. calculatorProxy.sub(5, 3);
  6. calculatorProxy.mul(3, 4);
  7. calculatorProxy.div(6, 1);

在配置文件中可以看到,使用了logAroundAdvice这个拦截器(我也不知道是不是叫拦截器)

  1. 配置文件中还要配置拦截器:
  2. <bean id="logAroundAdvice" class="myspring.aop.LogAroundAdvice" />

拦截器的实现类为:

  1. package myspring.aop;
  2. import org.aopalliance.intercept.MethodInterceptor;
  3. import org.aopalliance.intercept.MethodInvocation;
  4. public class LogAroundAdvice implements MethodInterceptor{
  5. //MethodInvocation相当于Method的包装类
  6. public Object invoke(MethodInvocation methodInvocation) throws Throwable {
  7. MyLogger logger = new MyLogger();
  8. try{
  9. logger.log("before: "+methodInvocation.getMethod().getName()+
  10. ",    args"+methodInvocation.getArguments());
  11. //须执行目标方法!
  12. Object result = methodInvocation.proceed();
  13. logger.log("after: "+methodInvocation.getMethod().getName());
  14. //注意返回目标方法的返回值
  15. return result;
  16. }
  17. catch(Exception e)
  18. {
  19. logger.log("Exception: "+e.getMessage());
  20. throw e;
  21. }
  22. }
  23. }

这个类实现了MethodInterceptor这个接口,表示在调用目标方法之前与之后都执行拦截。

以上拦截方式中完整的配置文件如下:

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5. <!-- Target -->
  6. <bean id="arithmeticCalculator" class="myspring.calculator.ArithmeticCalculatorImp" />
  7. <!-- Advice -->
  8. <bean id="logBeforeAdvice" class="myspring.aop.LogBeforeAdvice" />
  9. <bean id="logAfterReturning" class="myspring.aop.LogAfterReturningAdvice" />
  10. <bean id="logThrowsAdvice" class="myspring.aop.logThrowsAdvice" />
  11. <bean id="logAroundAdvice" class="myspring.aop.LogAroundAdvice" />
  12. <!-- Proxy -->
  13. <bean id="arithmeticCalculatorProxy"
  14. class="org.springframework.aop.framework.ProxyFactoryBean">
  15. <property name="target" ref="arithmeticCalculator"></property>
  16. <property name="interceptorNames">
  17. <list>
  18. <!-- <value>logBeforeAdvice</value>
  19. <value>logAfterReturning</value>
  20. <value>logThrowsAdvice</value> -->
  21. <value>logAroundAdvice</value>
  22. </list>
  23. </property>
  24. </bean>
  25. </beans>

以上的方法可以拦截目标类中的所有方法,如果只拦截指定方法就不能用这个了。

配置文件要换一下

  1. <bean id="arithmeticCalculatorProxy"
  2. class="org.springframework.aop.framework.ProxyFactoryBean">
  3. <property name="target" ref="arithmeticCalculator"></property>
  4. <property name="interceptorNames">
  5. <list>
  6. <value>nameMatchAdvisor</value>                             </list>
  7. </property>
  8. </bean>
  9. <bean id="nameMatchAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
  10. <property name="mappedNames">
  11. <list>
  12. <value>add</value><!--只拦截这两个方法-->
  13. <value>sub</value>
  14. </list>
  15. </property>
  16. <property name="advice" ref="logAroundAdvice" /><!--按照这个拦截器的方式拦截那两个方法-->
  17. </bean>

以上拦截方式中的完整配置文件如下:

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5. <!-- Target -->
  6. <bean id="arithmeticCalculator" class="myspring.calculator.ArithmeticCalculatorImp" />
  7. <!-- Advice -->
  8. <bean id="logBeforeAdvice" class="myspring.aop.LogBeforeAdvice" />
  9. <bean id="logAfterReturning" class="myspring.aop.LogAfterReturningAdvice" />
  10. <bean id="logThrowsAdvice" class="myspring.aop.logThrowsAdvice" />
  11. <bean id="logAroundAdvice" class="myspring.aop.LogAroundAdvice" />
  12. <bean id="nameMatchAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
  13. <property name="mappedNames">
  14. <list>
  15. <value>add</value>
  16. <value>sub</value>
  17. </list>
  18. </property>
  19. <property name="advice" ref="logAroundAdvice" />
  20. </bean>
  21. <bean id="arithmeticCalculatorProxy"
  22. class="org.springframework.aop.framework.ProxyFactoryBean">
  23. <property name="target" ref="arithmeticCalculator"></property>
  24. <property name="interceptorNames">
  25. <list>
  26. <value>nameMatchAdvisor</value>
  27. </list>
  28. </property>
  29. </bean>
  30. </beans>

转载于:https://blog.51cto.com/4045060/780566

spring中关于aop拦截功能的记录相关推荐

  1. 一文读懂Spring中的AOP机制

    一.前言 这一篇我们来说一下 Spring 中的 AOP 机制,为啥说完注解的原理然后又要说 AOP 机制呢? 1.标记日志打印的自定义注解 @Target({ElementType.METHOD}) ...

  2. 动态代理——》AOP —— Spring 中的 AOP||AOP 相关术语||学习 spring 中的 AOP 要明确的事

    AOP 概述 什么是 AOP       AOP:全称是 Aspect Oriented Programming 即:面向切面编程 AOP 的作用及优势 作用: 在程序运行期间,不修改源码对已有方法进 ...

  3. Spring中的AOP(三)——基于Annotation的配置方式(一)

    为什么80%的码农都做不了架构师?>>>    AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是 ...

  4. spring中的aop术语和细节

    Spring中AOP的细节 说明 我们学习spring的aop,就是通过配置的方式 AOP相关术语 Joinpoint(连接点): 所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法, ...

  5. java学习day40(Spring)spring中的aop和基于XML以及注解的AOP配置

    第1章 AOP 的相关概念[理解] 1.1AOP 概述 1.1.1 什么是 AOP AOP :全称是 Aspect Oriented Programming 即:面向切面编程. 简单的说它就是把我们程 ...

  6. 手动实现SPring中的AOP(1)

    Spring中的AOP是基于JDK的API动态的在内存中创建代理对象的.所以这里先介绍一些设计模式之----代理模式: a)         代理模式的定义:代理(Proxy)模式是一种提供对目标对象 ...

  7. java day59【 AOP 的相关概念[理解] 、 Spring 中的 AOP[掌握] 、 Spring 整合 Junit[掌握] 】...

    第1章 AOP 的相关概念[理解] 1.1AOP 概述 1.1.1 什么是 AOP 1.1.2 AOP 的作用及优势 1.1.3 AOP 的实现方式 1.2AOP 的具体应用 1.2.1 案例中问题 ...

  8. Spring 中的AOP的通知类型的示例(xml)

    个人博客:https://suveng.github.io/blog/​​​​​​​ Spring 中的AOP的通知类型的示例 AOP中的通知类型(advice)一共有五中: around advic ...

  9. Spring中的AOP切面编程的三种实现方式

    文章目录 Spring中的AOP切面编程的三种实现方式 1.最基本AOP的实现 a.引入jar包 b.编写通知类,这里以后置通知和环绕通知类为例子进行说明 c.在SpringIOC容器中配置 d.测试 ...

最新文章

  1. CentOS Linux内核升级全过程
  2. jupyter提示信息安装后正常OK的
  3. SpringSecurity使用自定义认证页面
  4. 澳大利亚.新西兰C#考试题
  5. 【性能测试】性能需求挖掘、性能方案制定及压测场景设计之疑惑与思考(一)
  6. 牛客网Wannafly挑战赛15 B车辆安排(模拟)AND C 出队(规律)
  7. 百万数据php7取出循环_Thinkphp5 分批批量导出百万条数据记录的Code,不用PHPEXCEL哦!...
  8. flash 音乐 html代码,用html为flash页面添加音乐
  9. SpringBoot2.x填坑(一):使用CROS解决跨域并解决swagger 访问不了问题
  10. mac使用Alfred搜索外接移动硬盘失败的解决方法
  11. 无线充电发热解决办法
  12. 第一章 SpringMvc---Web MVC简介
  13. SMAP土壤湿度数据的下载与hdf-tif转格式
  14. 简单的Java视频播放器
  15. 水浒歪传--郭德纲相声
  16. 软件项目管理-第三章生存期模型
  17. 谷歌浏览器自带的谷歌翻译无法使用的解决办法
  18. Java小白自学7:选择结构练习题
  19. java的字典序排序_java实现字典序排序
  20. Linux中重定向输入和输出

热门文章

  1. Quartz教程三:Job与JobDetail介绍
  2. Zabbix3.X---Zabbix _Agent主动模式的监控(Active)
  3. cxf开发Restful Web Services
  4. getAttribute与getParameter区别
  5. Cisco1242胖AP转瘦AP并加入Cisco无线控制器
  6. html可以导入MySQL吗_将数据从HTML文件(带有嵌入式JavaScript)导入MySQL数据库
  7. Java向MySQL数据库插入时间类型Date数据时需要注意的问题
  8. js 中对象--对象结构(原型链基础解析)
  9. CSS3 Media Queries详细介绍和使用实例
  10. Struts2学习笔记——Struts2与Spring整合