笔记13 AOP中After和AfterReturning的区别
AOP中 @Before @After @AfterThrowing@AfterReturning的执行顺序
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result;
try {
//@Before
result = method.invoke(target, args);
//@After
return result;
} catch (InvocationTargetException e) {
Throwable targetException = e.getTargetException();
//@AfterThrowing
throw targetException;
} finally {
//@AfterReturning
}
}
以Audience为例,代码如下:
package concert; import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Aspect
@Component
public class Audience {
@Pointcut("execution(* concert.Performance.perform(..))")
public void performs() { } @Before("performs()")
public void silenceCellPhones() {
System.out.println("Silencing cell phone");
} @Before("performs()")
public void takeSeats() {
System.out.println("Taking seats");
} @After("performs()")
public void after() {
System.out.println("after");
} @AfterReturning("performs()")
public void applause() {
System.out.println("CLAP CLAP CLAP");
} @AfterThrowing("performs()")
public void demandRefund() {
System.out.println("Demanding a refund");
}
}
执行结果:

注入AspectJ切面 (新)
1.将原来的观众类定义为一个真正的切面,Audience.java 将观众的行为都放在这个切面中,然后在spring中引用这个切面,并且为这个切面注入新的方法,具体可参照笔记12。
package concert4;
public aspect Audience {
public Audience(){}
pointcut performance():execution(* perform(..));
before():performance(){
System.out.println("Silencing cell phone");
}
before():performance(){
System.out.println("Taking seats");
}
after():performance(){
System.out.println("CLAP CLAP CLAP");
}
after()returning :performance(){
System.out.println("--------------评论----------------");
System.out.println(criticismEngine.getCriticism());
System.out.println("----------------------------------");
}
private CriticismEngine criticismEngine;
public void setCriticismEngine(CriticismEngine criticismEngine){
this.criticismEngine=criticismEngine;
}
}
2.修改XML配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean class="concert4.Classcial"></bean>
<bean id="criticismEngine" class="concert4.CriticismEngineTmpl">
<property name="criticismPool">
<list>
<value>不好</value>
<value>很不好</value>
<value>非常不好</value>
</list>
</property>
</bean>
<bean id="audience" class="concert4.Audience" factory-method="aspectOf">
<property name="criticismEngine" ref="criticismEngine"></property>
</bean>
<aop:config>
<aop:aspect ref="audience">
<aop:declare-parents types-matching="concert4.Performance+"
implement-interface="concert4.Encoreable"
default-impl="concert4.DefaultEncoreable"/>
</aop:aspect>
</aop:config> </beans>
3.结果

笔记13 AOP中After和AfterReturning的区别的更多相关文章
- Spring AOP中 args和arg-names的区别
这两天在看aop aspectj的各种语法,发现里面有两个概念 args和arg-names很容易混淆,网上也基本没说清楚,所以就动手试了一下,发现还是自己试试比较好理解 先说结论: args是和ex ...
- spring aop中aspect和advisor的区别
之前看到spring AOP配置aspect(切面)有两种方式,一种是利用注解的方式配置,一种是利用XML的方式配置. 我们的配置是这样的<aop:aspect>,还有另外一种<ao ...
- C语言学习笔记 (002) - C++中引用和指针的区别(转载)
下面用通俗易懂的话来概述一下: 指针-对于一个类型T,T*就是指向T的指针类型,也即一个T*类型的变量能够保存一个T对象的地址,而类型T是可以加一些限定词的,如const.volatile等等.见下图 ...
- 前端学习笔记之HTML中的id,name,class区别
name 属性用于在 JavaScript 中对元素进行引用,或者在表单提交之后,对表单数据进行引用. html的name和id可以类比身份证的姓名和身份证编号,编号id具有唯一性,一个id只出现一次 ...
- 【学习笔记】js中undefined和null的区别和联系
在JavaScript中存在这样两种原始类型:Null与Undefined.这两种类型常常会使JavaScript的开发人员产生疑惑,在什么时候是Null,什么时候又是Undefined? Undef ...
- 机器学习实战 - 读书笔记(13) - 利用PCA来简化数据
前言 最近在看Peter Harrington写的"机器学习实战",这是我的学习心得,这次是第13章 - 利用PCA来简化数据. 这里介绍,机器学习中的降维技术,可简化样品数据. ...
- Spring AOP中定义切点(PointCut)和通知(Advice)
如果你还不熟悉AOP,请先看AOP基本原理,本文的例子也沿用了AOP基本原理中的例子.切点表达式 切点的功能是指出切面的通知应该从哪里织入应用的执行流.切面只能织入公共方法.在Spring AOP中, ...
- Ext.Net学习笔记13:Ext.Net GridPanel Sorter用法
Ext.Net学习笔记13:Ext.Net GridPanel Sorter用法 这篇笔记将介绍如何使用Ext.Net GridPanel 中使用Sorter. 默认情况下,Ext.Net GridP ...
- Spring AOP高级——源码实现(2)Spring AOP中通知器(Advisor)与切面(Aspect)
本文例子完整源码地址:https://github.com/yu-linfeng/BlogRepositories/tree/master/repositories/Spring%20AOP%E9%A ...
随机推荐
- Faster R-CNN 的 RPN 是啥子?
 Faster R-CNN,由两个模块组成: 第一个模块是深度全卷积网络 RPN,用于 region proposal; 第二个模块是Fast R-CNN检测器,它使用了RPN产生的region p ...
- 直方图均衡化及matlab实现
在处理图像时,偶尔会碰到图像的灰度级别集中在某个小范围内的问题,这时候图像很难看清楚.比如下图: 它的灰度级别,我们利用一个直方图可以看出来(横坐标从0到255,表示灰度级别,纵坐标表示每个灰度级别的 ...
- day-3 python多线程编程知识点汇总
python语言以容易入门,适合应用开发,编程简洁,第三方库多等等诸多优点,并吸引广大编程爱好者.但是也存在一个被熟知的性能瓶颈:python解释器引入GIL锁以后,多CPU场景下,也不再是并行方式运 ...
- java.lang.String 类源码解读
String类定义实现了java.io.Serializable, Comparable<String>, CharSequence 三个接口:并且为final修饰. public fin ...
- 自动化服务部署(一):Linux下安装JDK
自动化测试的主要目的是为了执行回归测试.当然,为了模拟真实的用户操作,一般都是在UAT或者生产环境进行回归测试. 为了尽量避免内网和外网解析对测试结果的影响,将自动化测试服务部署在外网的服务器是比较好 ...
- myeclipse的导航器
在myeclipse的导航器下面可以看到编译后的文件目录结构 如何打开导航器试图呢? 窗口->显示视图->导航器 windows->show view->Navigator 这 ...
- 阿里云API网关(7)开发指南-API参考
网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...
- apigw鉴权分析(1-5)亚马逊 - 鉴权分析
一.访问入口 https://developer.amazon.com/public/zh 二.鉴权方式分析 三.分解结论
- 2018年Web前端自学路线
本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. Web前端入门的自学路线 新手入门前端,需要学习的基础内容有很多,如下. ...
- 路由测试-lee
//get 路由 Route::get('/', 'WelcomeController@index'); Route::get('home', 'HomeController@index'); //路 ...