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.*(..))"></aop:pointcut>
<aop:before method="aspectBefore" pointcut-ref="ikPoint"></aop:before>
</aop:aspect>
</aop:config>- 优点:前置通知、后置通知、环绕通知使用同一个切点时,配置一个<aop:poincut />即可,方便配置。
- 方式二
<aop:config>
<aop:aspect id="ikAspectAop" ref="ikAspect">
<aop:before method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:before>
</aop:aspect>
</aop:config>- 优点:前置通知、后置通知、环绕通知使用不同切点时,不需要配置<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.*(..))"></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.*(..))"></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.*(..))"></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.*(..))"></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.*(..))"></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.*(..))"></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.*(..))"></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.*(..))"></aop:before>
</aop:aspect>
</aop:config> - Next
- 无论切面是否出现异常,后通知动作正常执行
- 环绕通知(Around Advice)
- 环绕通知在切入点时机前后都可以执行通知。(在切面类中的通知方法中,默认存在参数ProceedingJoinPoint pj)
- 方式一
<?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.*(..))"></aop:pointcut>
<aop:around method="aspectAround" pointcut-ref="ikPoint"></aop:around>
</aop:aspect>
</aop:config> </beans>package com.jing.spring.aop;
import org.aspectj.lang.ProceedingJoinPoint;
/**
*切面类
*/
public class IKAspect {
public void aspectAround(ProceedingJoinPoint pj){ System.out.println("IKAspect.aspectAroud,its 环绕通知前");
try {
Object proceed = pj.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("IKAspect.aspectAroud,its 环绕通知后"); }
} - 方式二
<?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="aspectAround" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:around>
</aop:aspect>
</aop:config> </beans>package com.jing.spring.aop;
import org.aspectj.lang.ProceedingJoinPoint;
/**
*切面类
*/
public class IKAspect {
public void aspectAround(ProceedingJoinPoint pj){ System.out.println("IKAspect.aspectAroud,its 环绕通知前");
try {
Object proceed = pj.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("IKAspect.aspectAroud,its 环绕通知后"); }
} - Next
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 ...
随机推荐
- 如何组织AxTOCControl里面的数据
如何组织AxTOCControl里面的数据,实际上是组织AXMapControl的数据,将添加的数据进行整理.代码在最后面. 思路参考自: https://blog.csdn.net/u0124887 ...
- 简述移动端开发前端和app间的关系
<p>前端页面嵌套进app内部,一般有时候会进行一些交互,类似于前端页面请求后台接口一样,通常会起一个前端开发人员和app开发人员会相互协定一个协议;双方就协议而言去进行请求接口和返回数据 ...
- LG3369 普通平衡树
题意 维护一些数,其中需要提供以下操作: 1.插入\(x\) 2.删除\(x\)(若有多个相同的数,只删除一个) 3.查询\(x\)的排名(排名定义为比当前数小的数的个数\(+1\)) 4.查询排名为 ...
- .net拼接json字符串
{ while (reader.Read()) { if (reader.HasRows) { JSONstring += "{"; JSONstring += "\&q ...
- php7.2连接Sqlserver2008 r2
下载Sql Server PHP扩展 Microsoft Drivers for PHP for SQL Server https://github.com/Microsoft/msphpsql/re ...
- java中,什么是构造函数?什么是构造函数重载?什么是复制构造函数?
当新对象被创建的时候,构造函数会被调用.每一个类都有构造函数.在程序中没有给类提供构造函数的情况下,Java编译器会为这个类创建一个默认的构造函数 Java中构造函数重载和方法重载很相似.可以为一个类 ...
- 接口自动化框架(java)--4.接口Token传递
这套框架的报告是自己封装的 一般token会在登录接口返回结果中呈现,从代码层面获取token的方式有很多种,我是使用jsonpath这个json路径语言去匹配token所在路径的key值 packa ...
- 《linux就该这么学》第十一节课: 第九章,网卡绑定与sshd服务
8.4.服务的访问控制列表 TCPwrappers是RHEL7中默认启用的流量监控程序,能够对服务做出允许或拒绝. TCPwrappers服务由两个文件控制: /etc/hosts.allow ...
- Docker 共有 13 个管理命令和 41 个通用命令,以下是常用 Docker 命令列表
开发人员一直在努力提高 Docker 的使用率和性能,命令也在不停变化.Docker 命令经常被弃用,或被替换为更新且更有效的命令,本文总结了近年来资深专家最常用的命令列表并给出部分使用方法. 目前, ...
- 【数据结构】算法 LinkList (Insertion Sort List 链表插入排序)
将一个单链表进行处理后,所得结果为一有序链表 Solution: 将原始链表逐个查询,插入新链表,在插入的同时对链表进行排序.时间复杂度O(n*n) public ListNode insertion ...