spring中关于aop拦截功能的记录
java写了个接口并写了个实现类:
- package myspring.calculator;
- public interface IArithmeticCalculator {
- public double add(double a, double b);
- public double sub(double a, double b);
- public double mul(double a, double b);
- public double div(double a, double b);
- }
- package myspring.calculator;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- public class ArithmeticCalculatorImp implements IArithmeticCalculator {
- public double add(double a, double b) {
- double result = a + b;
- System.out.println(a + " + " + b + " = " + result);
- return result;
- }
- public double sub(double a, double b) {
- double result = a - b;
- System.out.println(a + " - " + b + " = " + result);
- return result;
- }
- public double mul(double a, double b) {
- double result = a * b;
- System.out.println(a + " * " + b + " = " + result);
- return result;
- }
- public double div(double a, double b) {
- if (b == 0) {
- throw new IllegalArgumentException("Division by zero");
- }
- double result = a / b;
- System.out.println(a + " / " + b + " = " + result);
- return result;
- }
- }
在spring配置文件aop-base.xml中配置好
- <bean id="arithmeticCalculator" class="myspring.calculator.ArithmeticCalculatorImp" />
- <bean id="arithmeticCalculatorProxy"
- class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="target" ref="arithmeticCalculator"></property>
- <property name="interceptorNames"><!--定义使用何种方式拦截-->
- <list>
- <!-- <value>logBeforeAdvice</value>
- <value>logAfterReturning</value>
- <value>logThrowsAdvice</value> -->
- <value>logAroundAdvice</value>
- </list>
- </property>
- </bean>
在用到接口里方法的地方加载配置文件并获得代理类:
- ApplicationContext ac = new ClassPathXmlApplicationContext("aop-base.xml");
- IArithmeticCalculator calculatorProxy =
- (IArithmeticCalculator)ac.getBean("arithmeticCalculatorProxy");
- calculatorProxy.add(2, 3);
- calculatorProxy.sub(5, 3);
- calculatorProxy.mul(3, 4);
- calculatorProxy.div(6, 1);
在配置文件中可以看到,使用了logAroundAdvice这个拦截器(我也不知道是不是叫拦截器)
- 配置文件中还要配置拦截器:
- <bean id="logAroundAdvice" class="myspring.aop.LogAroundAdvice" />
拦截器的实现类为:
- package myspring.aop;
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- public class LogAroundAdvice implements MethodInterceptor{
- //MethodInvocation相当于Method的包装类
- public Object invoke(MethodInvocation methodInvocation) throws Throwable {
- MyLogger logger = new MyLogger();
- try{
- logger.log("before: "+methodInvocation.getMethod().getName()+
- ", args"+methodInvocation.getArguments());
- //须执行目标方法!
- Object result = methodInvocation.proceed();
- logger.log("after: "+methodInvocation.getMethod().getName());
- //注意返回目标方法的返回值
- return result;
- }
- catch(Exception e)
- {
- logger.log("Exception: "+e.getMessage());
- throw e;
- }
- }
- }
这个类实现了MethodInterceptor这个接口,表示在调用目标方法之前与之后都执行拦截。
以上拦截方式中完整的配置文件如下:
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <!-- Target -->
- <bean id="arithmeticCalculator" class="myspring.calculator.ArithmeticCalculatorImp" />
- <!-- Advice -->
- <bean id="logBeforeAdvice" class="myspring.aop.LogBeforeAdvice" />
- <bean id="logAfterReturning" class="myspring.aop.LogAfterReturningAdvice" />
- <bean id="logThrowsAdvice" class="myspring.aop.logThrowsAdvice" />
- <bean id="logAroundAdvice" class="myspring.aop.LogAroundAdvice" />
- <!-- Proxy -->
- <bean id="arithmeticCalculatorProxy"
- class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="target" ref="arithmeticCalculator"></property>
- <property name="interceptorNames">
- <list>
- <!-- <value>logBeforeAdvice</value>
- <value>logAfterReturning</value>
- <value>logThrowsAdvice</value> -->
- <value>logAroundAdvice</value>
- </list>
- </property>
- </bean>
- </beans>
以上的方法可以拦截目标类中的所有方法,如果只拦截指定方法就不能用这个了。
配置文件要换一下
- <bean id="arithmeticCalculatorProxy"
- class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="target" ref="arithmeticCalculator"></property>
- <property name="interceptorNames">
- <list>
- <value>nameMatchAdvisor</value> </list>
- </property>
- </bean>
- <bean id="nameMatchAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
- <property name="mappedNames">
- <list>
- <value>add</value><!--只拦截这两个方法-->
- <value>sub</value>
- </list>
- </property>
- <property name="advice" ref="logAroundAdvice" /><!--按照这个拦截器的方式拦截那两个方法-->
- </bean>
以上拦截方式中的完整配置文件如下:
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <!-- Target -->
- <bean id="arithmeticCalculator" class="myspring.calculator.ArithmeticCalculatorImp" />
- <!-- Advice -->
- <bean id="logBeforeAdvice" class="myspring.aop.LogBeforeAdvice" />
- <bean id="logAfterReturning" class="myspring.aop.LogAfterReturningAdvice" />
- <bean id="logThrowsAdvice" class="myspring.aop.logThrowsAdvice" />
- <bean id="logAroundAdvice" class="myspring.aop.LogAroundAdvice" />
- <bean id="nameMatchAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
- <property name="mappedNames">
- <list>
- <value>add</value>
- <value>sub</value>
- </list>
- </property>
- <property name="advice" ref="logAroundAdvice" />
- </bean>
- <bean id="arithmeticCalculatorProxy"
- class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="target" ref="arithmeticCalculator"></property>
- <property name="interceptorNames">
- <list>
- <value>nameMatchAdvisor</value>
- </list>
- </property>
- </bean>
- </beans>
转载于:https://blog.51cto.com/4045060/780566
spring中关于aop拦截功能的记录相关推荐
- 一文读懂Spring中的AOP机制
一.前言 这一篇我们来说一下 Spring 中的 AOP 机制,为啥说完注解的原理然后又要说 AOP 机制呢? 1.标记日志打印的自定义注解 @Target({ElementType.METHOD}) ...
- 动态代理——》AOP —— Spring 中的 AOP||AOP 相关术语||学习 spring 中的 AOP 要明确的事
AOP 概述 什么是 AOP AOP:全称是 Aspect Oriented Programming 即:面向切面编程 AOP 的作用及优势 作用: 在程序运行期间,不修改源码对已有方法进 ...
- Spring中的AOP(三)——基于Annotation的配置方式(一)
为什么80%的码农都做不了架构师?>>> AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是 ...
- spring中的aop术语和细节
Spring中AOP的细节 说明 我们学习spring的aop,就是通过配置的方式 AOP相关术语 Joinpoint(连接点): 所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法, ...
- java学习day40(Spring)spring中的aop和基于XML以及注解的AOP配置
第1章 AOP 的相关概念[理解] 1.1AOP 概述 1.1.1 什么是 AOP AOP :全称是 Aspect Oriented Programming 即:面向切面编程. 简单的说它就是把我们程 ...
- 手动实现SPring中的AOP(1)
Spring中的AOP是基于JDK的API动态的在内存中创建代理对象的.所以这里先介绍一些设计模式之----代理模式: a) 代理模式的定义:代理(Proxy)模式是一种提供对目标对象 ...
- 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 案例中问题 ...
- Spring 中的AOP的通知类型的示例(xml)
个人博客:https://suveng.github.io/blog/ Spring 中的AOP的通知类型的示例 AOP中的通知类型(advice)一共有五中: around advic ...
- Spring中的AOP切面编程的三种实现方式
文章目录 Spring中的AOP切面编程的三种实现方式 1.最基本AOP的实现 a.引入jar包 b.编写通知类,这里以后置通知和环绕通知类为例子进行说明 c.在SpringIOC容器中配置 d.测试 ...
最新文章
- CentOS Linux内核升级全过程
- jupyter提示信息安装后正常OK的
- SpringSecurity使用自定义认证页面
- 澳大利亚.新西兰C#考试题
- 【性能测试】性能需求挖掘、性能方案制定及压测场景设计之疑惑与思考(一)
- 牛客网Wannafly挑战赛15 B车辆安排(模拟)AND C 出队(规律)
- 百万数据php7取出循环_Thinkphp5 分批批量导出百万条数据记录的Code,不用PHPEXCEL哦!...
- flash 音乐 html代码,用html为flash页面添加音乐
- SpringBoot2.x填坑(一):使用CROS解决跨域并解决swagger 访问不了问题
- mac使用Alfred搜索外接移动硬盘失败的解决方法
- 无线充电发热解决办法
- 第一章 SpringMvc---Web MVC简介
- SMAP土壤湿度数据的下载与hdf-tif转格式
- 简单的Java视频播放器
- 水浒歪传--郭德纲相声
- 软件项目管理-第三章生存期模型
- 谷歌浏览器自带的谷歌翻译无法使用的解决办法
- Java小白自学7:选择结构练习题
- java的字典序排序_java实现字典序排序
- Linux中重定向输入和输出
热门文章
- Quartz教程三:Job与JobDetail介绍
- Zabbix3.X---Zabbix _Agent主动模式的监控(Active)
- cxf开发Restful Web Services
- getAttribute与getParameter区别
- Cisco1242胖AP转瘦AP并加入Cisco无线控制器
- html可以导入MySQL吗_将数据从HTML文件(带有嵌入式JavaScript)导入MySQL数据库
- Java向MySQL数据库插入时间类型Date数据时需要注意的问题
- js 中对象--对象结构(原型链基础解析)
- CSS3 Media Queries详细介绍和使用实例
- Struts2学习笔记——Struts2与Spring整合