笔记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 ...
随机推荐
- HTML 样式设计
1.自动设置外边距 style="margin:auto auto;"
- JS页面跳转的常用方法整理.
<script type="text/javascript"> //js页面跳转 function showtabs() { window.location.href ...
- Echarts 折线图y轴标签值太长时显示不全的解决办法
问题 分析 解决办法 问题 先看一下正常的情况 再看一下显示不全的情况 所有的数据都是从后台取的,也就是说动态变化的,一开始的时候数据量不大不会出现问题,后面y轴的值越来越大的时候就出现了这个显示不全 ...
- Lock(三)查看是谁把表给锁了
查看是谁把表给锁了 select se1.inst_id as 被阻塞的会话节点, se2.inst_id as 罪魁祸首节点, se1.sid as 被阻塞的会话ID, ob.object_name ...
- JQ 标签相关知识
1.判断 checkbox 和 radio 是否选中 if($("标签选择器").is(":checked")) 2.改变 checkbox 选中状态 .pro ...
- Python内置函数(50)——issubclass
英文文档: issubclass(class, classinfo) Return true if class is a subclass (direct, indirect or virtual) ...
- SSH 配置
ssh免密通用配置 Host * Port 1234 User root #ProxyCommand nc -X 5 -x 127.0.0.1:1081 %h %p #5 socks5, 4 sock ...
- spring-oauth-server实践:access_token的有效期分析
1.access_token有效期检查 用expiration和new Date()比较!!!!!! 分析目标-->expiration什么时候设置,设置规则如何配置!!!!!!! 2.acce ...
- 新概念英语(1-51)A pleasant climate
新概念英语(1-51)A pleasant climate Does it ever snow in Greece? A:Where do you come from? B:I come from G ...
- nodejs调试总结
之前nodejs开发中最痛苦的就是调试,因为我之前开发node时使用的编辑器还没有将nodejs的调试也集成进去,所以简单对nodejs开发的调试做了点探索,nodejs本身就有调试功能,同时node ...