Spring 学习——Spring AOP——AOP配置篇Advice(有参数传递)
声明通知Advice
- 配置方式(以前置通知为例子)
- 方式一
<aop:config>
<aop:aspect id="ikAspectAop" ref="ikAspect">
<aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:pointcut>
<aop:before method="aspectBefore" pointcut-ref="ikPoint"></aop:before>
</aop:aspect>
</aop:config>package com.jing.spring.aop;
/**
* 业务代码
*/
public class IKAspectBiz { public void add(String name){
System.out.println("IKAspectBiz.add");
}
}package com.jing.spring.aop;
import org.aspectj.lang.ProceedingJoinPoint; /**
* 切面代码
*/
public class IKAspect { public void aspectBefore(String name){
System.out.println("IKAspect.aspectBefore,it~s 前置通知");
System.out.println("IKAspect.aspectBefore,it~s 前置通知"+name);
}
}- 优点:前置通知、后置通知、环绕通知使用同一个切点时,配置一个<aop:poincut />即可,方便配置。
- 方式二
<aop:config>
<aop:aspect id="ikAspectAop" ref="ikAspect">
<aop:before method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:before>
</aop:aspect>
</aop:config>package com.jing.spring.aop;
/**
* 业务代码
*/
public class IKAspectBiz { public void add(String name){
System.out.println("IKAspectBiz.add");
}
}package com.jing.spring.aop;
import org.aspectj.lang.ProceedingJoinPoint; /**
* 切面代码
*/
public class IKAspect { public void aspectBefore(String name){
System.out.println("IKAspect.aspectBefore,it~s 前置通知");
System.out.println("IKAspect.aspectBefore,it~s 前置通知"+name);
}
}- 优点:前置通知、后置通知、环绕通知使用不同切点时,不需要配置<aop:poincut />元素,可以直接在<aop:before />元素内配置切点。
- Next
- 方式一
- 前置通知(Before Advice)
- 在切入点时机事务执行之前,执行通知
- 方式一
<aop:config>
<aop:aspect id="ikAspectAop" ref="ikAspect">
<aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:pointcut>
<aop:before method="aspectBefore" pointcut-ref="ikPoint"></aop:before>
</aop:aspect>
</aop:config> - 方式二
<aop:config>
<aop:aspect id="ikAspectAop" ref="ikAspect">
<aop:before method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:before>
</aop:aspect>
</aop:config> - Next
- 后置通知(After Running Advice)
- 在切入点时机事务执行之后,执行通知(前提:切入点执行时没有异常,否则不会执行)
- 方式一
<aop:config>
<aop:aspect id="ikAspectAop" ref="ikAspect">
<aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:pointcut>
<aop:after-returning method="aspectBefore" pointcut-ref="ikPoint"></aop:before>
</aop:aspect>
</aop:config> - 方式二
<aop:config>
<aop:aspect id="ikAspectAop" ref="ikAspect">
<aop:after-returning method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:before>
</aop:aspect>
</aop:config> - Next
- 抛出异常通知(After Throwing Advice)
- 若在切入点时机执行时抛出异常,执行通知。(执行异常通知,则不会执行后置通知)
- 方式一
<aop:config>
<aop:aspect id="ikAspectAop" ref="ikAspect">
<aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name) "></aop:pointcut>
<aop:after-throwing method="aspectBefore" pointcut-ref="ikPoint"></aop:before>
</aop:aspect>
</aop:config> - 方式二
<aop:config>
<aop:aspect id="ikAspectAop" ref="ikAspect">
<aop:after-throwing method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:before>
</aop:aspect>
</aop:config> - Next
- 后通知(After Ending Advice)
- 在切入点时机执行之后,执行通知。(切入点执行时,无论正常执行,还是抛出异常,都会执行通知,相当于try_catch_finally)。
- 方式一
<aop:config>
<aop:aspect id="ikAspectAop" ref="ikAspect">
<aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:pointcut>
<aop:after method="aspectBefore" pointcut-ref="ikPoint"></aop:before>
</aop:aspect>
</aop:config> - 方式二
<aop:config>
<aop:aspect id="ikAspectAop" ref="ikAspect">
<aop:after method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:before>
</aop:aspect>
</aop:config> - Next
- 无论切面是否出现异常,后通知动作正常执行
- 环绕通知(Around Advice)
- 环绕通知在
- 方式一
<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd" > <bean id="ikAspect" class="com.jing.spring.aop.IKAspect"></bean>
<bean id="ikAspectBiz" class="com.jing.spring.aop.IKAspectBiz"></bean> <aop:config>
<aop:aspect id="ikAspectAop" ref="ikAspect">
<aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.aopParams(String,int)) and args(name,times)"></aop:pointcut>
<aop:around method="aspectAroundParams" pointcut-ref="ikPoint"></aop:around>
</aop:aspect>
</aop:config> </beans>package com.jing.spring.aop; /**
* 业务代码
*/
public class IKAspectBiz { public void aopParams(String name,int times){
System.out.println("IKAspectBiz.aopParams");
}
}package com.jing.spring.aop;
import org.aspectj.lang.ProceedingJoinPoint; /**
* 切面代码
*/
public class IKAspect {
public void aspectAroundParams(ProceedingJoinPoint pj,String name,int times){ System.out.println("IKAspect.aspectAroud,its 环绕通知前:");
System.out.println("IKAspect.aspectAroud,"+name+"-----"+times);
try {
Object proceed = pj.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("IKAspect.aspectAroud,its 环绕通知后");
System.out.println("IKAspect.aspectAroud,"+name+"-----"+times); }
} - 方式二
<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd" > <bean id="ikAspect" class="com.jing.spring.aop.IKAspect"></bean>
<bean id="ikAspectBiz" class="com.jing.spring.aop.IKAspectBiz"></bean> <aop:config>
<aop:aspect id="ikAspectAop" ref="ikAspect">
<aop:around method="aspectAroundParams" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.aopParams(String,int)) and args(name,times)"></aop:around>
</aop:aspect>
</aop:config> </beans>package com.jing.spring.aop; /**
* 业务代码
*/
public class IKAspectBiz { public void aopParams(String name,int times){
System.out.println("IKAspectBiz.aopParams");
}
}package com.jing.spring.aop;
import org.aspectj.lang.ProceedingJoinPoint; /**
* 切面代码
*/
public class IKAspect { public void aspectAroundParams(ProceedingJoinPoint pj,String name,int times){ System.out.println("IKAspect.aspectAroud,its 环绕通知前:");
System.out.println("IKAspect.aspectAroud,"+name+"-----"+times);
try {
Object proceed = pj.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("IKAspect.aspectAroud,its 环绕通知后");
System.out.println("IKAspect.aspectAroud,"+name+"-----"+times); }
} - Next
谨记
- 切面中的参数来源是从业务中取得的,也就是说,只有在切点中的参数,才会传送到相对应的切面中,所以,切面中方法参数和切点时机的参数要保持对应。
<aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.aopParams(String,int)) and args(name,times)"></aop:pointcut>
<aop:around method="aspectAroundParams" pointcut-ref="ikPoint"></aop:around>
- (以环绕通知举例)上述中的args(name,times)中的参数name对应切面IKAspect 类中的aspectAroundParams(ProceedingJoinPoint pj,String name,int times)方法中的参数,并且参数名称和顺序需要与切面中的通知方法参数名称和顺序一致。
- (以环绕通知举例)上述中的aopParams(String,int)中的参数是对应业务IKAspectBiz 类中的aopParams(String name,int times)方法中的参数类型。
Spring 学习——Spring AOP——AOP配置篇Advice(有参数传递)的更多相关文章
- Spring学习笔记之aop动态代理(3)
Spring学习笔记之aop动态代理(3) 1.0 静态代理模式的缺点: 1.在该系统中有多少的dao就的写多少的proxy,麻烦 2.如果目标接口有方法的改动,则proxy也需要改动. Person ...
- Spring 学习——Spring AOP——AOP概念篇
AOP AOP的定义:AOP,Aspect Oriented Programming的缩写,意为面向切面编程,是通过预编译或运行期动态代理实现程序功能处理的统一维护的一种技术 实现方式 预编译 Asp ...
- spring学习06(AOP)
9.AOP 什么是AOP AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软 ...
- Spring学习笔记4——AOP
AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...
- Spring学习--基于 XML 的配置声明切面
正常情况下 , 基于注解的生命要优先于基于 XML 的声明. 通过 AspectJ 注解 , 切面可以与 AspectJ 兼容 , 而基于 XML 的配置则是 Spring 专有的.由于 Aspect ...
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring学习七----------Bean的配置之自动装配
© 版权声明:本文为博主原创文章,转载请注明出处 Bean的自动装配(Autowiring) no:不启用自动装配,此时需要手动注入.参考:Spring学习三----------注入方式 defaul ...
- Spring Boot 探索系列 - 自动化配置篇
26. Logging Prev Part IV. Spring Boot features Next 26. Logging Spring Boot uses Commons Logging f ...
- Spring学习八----------Bean的配置之Resources
© 版权声明:本文为博主原创文章,转载请注明出处 Resources 针对于资源文件的统一接口 -UrlResource:URL对应的资源,根据一个URL地址即可创建 -ClassPathResour ...
随机推荐
- 设计、定义并实现Complex类
设计.定义并实现Complex类 #include <iostream> #include <cmath> using namespace std; class MyCompl ...
- python生成数据后,快速导入数据库
1.使用python生成数据库文件内容 # coding=utf-8import randomimport time def create_user(): start = time.time() ...
- VM中centos设置子网内虚拟机ip
,首先应该设置vm的网络,打开编辑->虚拟网络编辑器,弹出框及具体设置如下图 2,选中相应的虚拟机,右键设置,并设置相应的选项,具体设置如下图 3.设置完成后,打开虚拟机,等待虚拟机启动后,输入 ...
- 【转】Spring Boot 构建应用——快速构建 Spring Boot 应用
Spring Boot 简化了 Spring 应用开发,不需要配置就能运行 Spring 应用,Spring Boot 的自动配置是通过 Spring 4.x 的条件注解 @Conditional 来 ...
- 2018-2019-2 20165236郭金涛《网络对抗》Exp1 PC平台逆向破解
2018-2019-2 20165236郭金涛<网络对抗>Exp1 PC平台逆向破解 一.实验内容 1.掌握NOP, JNE, JE, JMP, CMP汇编指令的机器码(0.5分) 2.掌 ...
- Cache Aside Pattern
Cache Aside Pattern 即旁路缓存是缓存方案的经验实践,这个实践又分读实践,写实践 对于读请求 先读cache,再读db 如果,cache hit,则直接返回数据 如果,cache m ...
- Chrome Inspect调试微信出现空白页面的解决方法
首先,需要打开手机的USB调试和微信的TBS 调试开关. 如果不打开TBS开关,Inspect时会检测不到任何微信的H5页面 使用微信扫码下方二维码,打开TBS调试开关: 普通网页: 小程序: 微信扫 ...
- html 类似雷达扫描效果 及 闪屏效果
//雷达扫描效果 1 <em id="Radar" class="RadarFast"></em> css: .RadarFast{ p ...
- python数据类型之元组类型
#为何要有元组,存放多个值,元组不可变,更多的是用来做查询 t=(1,[1,2,3],'a',(1,2)) #t=tuple((1,[1,2,3],'a',(1,2))) # print(type(t ...
- if语句&switch&Scanner
if流程控制语句: if 语句的用语法如下: if(boolean表达式){ //如果条件为true那么执行 } 只有条件为true时才会执行,否则执行if语句后面的代码. 实例代码: public ...