Spring5参考指南:基于Schema的AOP
基于Schema的AOP
上篇文章我们讲到了使用注解的形式来使用Spring AOP。本文我们介绍如何使用XML Schema的形式使用Spring AOP。
要想使用本文的AOP命名空间标记,需要导入xmlns:aop=“http://www.springframework.org/schema/aop”。
在Spring配置中,所有Aspect和Advisor元素都必须放在aop:config元素中(在应用程序上下文配置中可以有多个aop:config元素)。aop:config元素可以包含pointcut、advisor和aspect元素(请注意,这些元素必须按该顺序声明)。
定义Aspect
一个aspect是定义在Spring应用程序上下文的java bean对象。
你可以使用aop:aspect元素声明一个方面,并使用ref属性引用相应的bean,如下示例所示:
<aop:config>
<aop:aspect id="concurrentOperationRetry" ref="concurrentOperationExecutor">
</aop:aspect>
</aop:config>
<bean id="concurrentOperationExecutor"
class="com.flydean.aspect.ConcurrentOperationExecutor">
<property name="maxRetries" value="3"/>
<property name="order" value="100"/>
</bean>
定义Pointcut
你可以在aop:config中使用aop:pointcut来定义一个Pointcut,如下所示:
<aop:config>
<aop:pointcut id="idempotentOperation"
expression="execution(* com.flydean.service.*.*(..))
and
@annotation(com.flydean.beans.Idempotent)"/>
</aop:config>
定义在顶层aop:config中的aop:pointcut可以在多个aspects和advisors之间共享。
当组合切入点子表达式时,可以使用and、or和not关键字来代替&& || 和!,如下所示:
<aop:pointcut id="businessService"
expression="execution(* com.xyz.myapp.service.*.*(..)) && this(service)"/>
<aop:pointcut id="businessService"
expression="execution(* com.xyz.myapp.service.*.*(..)) and this(service)"/>
定义Advice
schema-based AOP 支持使用与@Aspectj样式相同的五种建议,它们具有完全相同的语义。
Before Advice
在匹配的方法执行之前运行通知。它通过在aop:aspect中声明使用的aop:before元素,如下示例所示:
<aop:before
pointcut-ref="dataAccessOperation"
method="doAccessCheck"/>
After Returning Advice
After Returning Advice,在匹配的方法执行正常完成时运行。它在一个aop:aspect中声明,方式与之前的通知相同。下面的示例演示如何声明它:
<aop:after-returning
pointcut-ref="dataAccessOperation"
method="doAccessCheck"/>
正如在@Aspectj样式中一样,您可以在通知正文中获得返回值。为此,请使用returning来指定应将返回值传递到的参数的名称,如下示例所示:
<aop:after-returning
pointcut-ref="dataAccessOperation"
returning="retVal"
method="doAccessCheck"/>
doAccessCheck方法必须要有声明名为retval的参数。如下所示:
public void doAccessCheck(Object retVal) {...
After Throwing Advice
当匹配的方法引发异常退出时执行。它通过在aop:aspect中声明after-throwing 元素来实现,如下示例所示:
<aop:after-throwing
pointcut-ref="dataAccessOperation"
method="doRecoveryActions"/>
同样的,你可以在通知方法中获得抛出的异常,如下所示:
<aop:after-throwing
pointcut-ref="dataAccessOperation"
throwing="dataAccessEx"
method="doRecoveryActions"/>
doRecoveryActions方法必须有声明名为DataAccessEx的参数,如下所示:
public void doRecoveryActions(DataAccessException dataAccessEx) {...
After (Finally) Advice
无论匹配的方法执行如何退出,after(finally)通知都会运行。可以使用after元素声明它,如下示例所示:
<aop:after
pointcut-ref="dataAccessOperation"
method="doReleaseLock"/>
Around Advice
最后一种advice是around advice的。around通知运行“around”匹配的方法执行。它有机会在方法执行之前和之后都进行工作,并确定何时、如何以及该方法真正开始执行。
你可以使用aop:around元素来声明around advice。advice方法的第一个参数必须是ProceedingJoinPoint类型。
<aop:around
pointcut-ref="businessService"
method="doBasicProfiling"/>
doBasicProfiling advice的实现如下:
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
// start stopwatch
Object retVal = pjp.proceed();
// stop stopwatch
return retVal;
}
advice参数
如果您希望显式地为advice方法指定参数名,可以使用advice元素的arg-names属性来指定参数名,下面是例子:
<aop:config>
<aop:aspect ref="profiler">
<aop:pointcut id="theExecutionOfSomePersonServiceMethod"
expression="execution(* com.flydean.service.PersonService.getPerson(String,int))
and args(name, age)"/>
<aop:around pointcut-ref="theExecutionOfSomePersonServiceMethod"
method="profile"/>
</aop:aspect>
</aop:config>
相应的aspect bean定义如下:
public class SimpleProfiler {
public Object profile(ProceedingJoinPoint call, String name, int age) throws Throwable {
StopWatch clock = new StopWatch("Profiling for '" + name + "' and '" + age + "'");
try {
clock.start(call.toShortString());
return call.proceed();
} finally {
clock.stop();
System.out.println(clock.prettyPrint());
}
}
}
profile接收两个参数。
Advisors
“Advisors”的概念来自于Spring中定义的AOP支持,在AspectJ中没有直接的等价物。Advisors就像一个独立的小方面,只有一条advice。
Spring使用aop:advisor元素支持Advisor概念。您通常会看到它与事务性advice结合使用,后者在Spring中也有自己的名称空间支持。以下示例展示了advisor:
<aop:config>
<aop:pointcut id="businessService"
expression="execution(* com.xyz.myapp.service.*.*(..))"/>
<aop:advisor
pointcut-ref="businessService"
advice-ref="tx-advice"/>
</aop:config>
<tx:advice id="tx-advice">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
本文的例子可以参考aop2
更多教程请参考 flydean的博客
Spring5参考指南:基于Schema的AOP的更多相关文章
- Spring5参考指南:基于注解的容器配置
文章目录 @Required @Autowired @primary @Qualifier 泛型 @Resource @PostConstruct和@PreDestroy Spring的容器配置可以有 ...
- Spring5参考指南:IOC容器
文章目录 为什么使用Spring5 什么是IOC容器 配置元数据 实例化容器 XML嵌套 groovy bean定义DSL 使用容器 最近在翻译Spring Framework Documentati ...
- 基于Schema的AOP 配置使用详解
原文地址:http://jinnianshilongnian.iteye.com/blog/1418598 基于Schema的AOP从Spring2.0之后通过"aop"命名空间来 ...
- 开涛spring3(6.3) - AOP 之 6.3 基于Schema的AOP
6.3 基于Schema的AOP 基于Schema的AOP从Spring2.0之后通过“aop”命名空间来定义切面.切入点及声明通知. 在Spring配置文件中,所以AOP相关定义必须放在<a ...
- spring aop 基于schema的aop
AOP的基本概念: 连接点(Jointpoint):表示需要在程序中插入横切关注点的扩展点,连接点可能是类初始化.方法执行.方法调用.字段调用或处理异常等等,Spring只支持方法执行连接点,在AOP ...
- spring3: 基于Schema的AOP
6.3 基于Schema的AOP 基于Schema的AOP从Spring2.0之后通过“aop”命名空间来定义切面.切入点及声明通知. 在Spring配置文件中,所以AOP相关定义必须放在<a ...
- Spring5参考指南:AOP代理
文章目录 AOP代理 AOP Proxies原理 AOP代理 通常来说Spring AOP有两种代理方式,一种默认的JDK代理,只能代理接口,一种是CGLIB代理,可以代理具体的类对象. Spring ...
- 第三章 AOP 基于Schema的AOP
基于Schema定义的切面和前现两种方式定义的切面,内容上都差不多,只是表现形式不一样而已. 3.7.1一般增强的使用 a.目标类 public class Target { public void ...
- Spring5参考指南:Bean作用域
文章目录 Bean作用域简介 Singleton作用域 Prototype作用域 Singleton Beans 中依赖 Prototype-bean web 作用域 Request scope Se ...
随机推荐
- Activiti7新的API介绍
一.Activiti7 的组成部分 Activiti Core 作为Activiti 的核心部分,Activiti Cloud 主要是利用云服务来实现分布式业务流程开发. 二.Activiti 新的 ...
- Vertica的这些事(四)——-vertica加密数据
通过创建 Secure Access Policies可以对vertica中的某一列数据进行加密: CREATE ACCESS POLICY ON [schema][tablename] FOR CO ...
- STM32F103ZET6窗口看门狗
1.WWDG简介 窗口看门狗(WWDG)通常被用来检测由外部干扰或不可预见的逻辑条件造成的应用程序背离正常的运行序列而产生的软件故障. WWDG是一个不断往下递减的计数器.当WWDG的计数器递减到固定 ...
- js 中对于this 的理解的 经典案例
function Foo(){ getName = function(){console.log(1);}; return this; }Foo.getName = function(){consol ...
- JavaScript中数组的两种排序方法详解(冒泡排序和选择排序)
一.冒泡排序的原理(从小到大) 相邻两个数进行比较,如果前一个数大于后一个数,那么就交换,否则不交换 原理剖析 比如有一组含有6个数字的数:5.3.7.2.1.6一共6个数字,做5次循环,每次循环相邻 ...
- 【php】日期时间
一. 日期时间: a) 这是一块非常重要的内容,我们在windows当中,或者是将来要接触的定时器也好,都是需要使用到这一块内容的!二. PHP当中的日期时间: a) 时间戳:time()可以获取时间 ...
- 微信小程序实现滑动tab切换和点击tab切换并显示相应的数据(附源代码)
这里主要用到了swiper组件和三目运算,直接上代码, 样式只有三个class,简单粗暴,懒的小伙伴们可以直接拿来用,喜欢的点个支持 <view> <view class=" ...
- Linq下有一个非常实用的SelectMany方法,很多人却不会用
在平时开发中经常会看到有些朋友或者同事在写代码时会充斥着各种for,foreach,这种程式代码太多的话阅读性特别差,而且还显得特别累赘,其实在FCL中有很多帮助我们提高阅读感的方法,而现实中很多人不 ...
- Unity - 旋转方法
前言 本文梳理了Unity中常用的旋转方法,涉及两大类:Transform.Quaternion. Transform 类 Rotate() 此方法重载多,易理解,在连续动态旋转中较为常用. /* o ...
- go 接口与动态类型
Go 没有类:数据(结构体或更一般的类型)和方法是一种松耦合的正交关系. 二.动态方法调用 通常需要编译器静态检查的支持:当变量被赋值给一个接口类型的变量时,编译器会检查其是否实现了该接口的所有函数.