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的区别的更多相关文章

  1. Spring AOP中 args和arg-names的区别

    这两天在看aop aspectj的各种语法,发现里面有两个概念 args和arg-names很容易混淆,网上也基本没说清楚,所以就动手试了一下,发现还是自己试试比较好理解 先说结论: args是和ex ...

  2. spring aop中aspect和advisor的区别

    之前看到spring AOP配置aspect(切面)有两种方式,一种是利用注解的方式配置,一种是利用XML的方式配置. 我们的配置是这样的<aop:aspect>,还有另外一种<ao ...

  3. C语言学习笔记 (002) - C++中引用和指针的区别(转载)

    下面用通俗易懂的话来概述一下: 指针-对于一个类型T,T*就是指向T的指针类型,也即一个T*类型的变量能够保存一个T对象的地址,而类型T是可以加一些限定词的,如const.volatile等等.见下图 ...

  4. 前端学习笔记之HTML中的id,name,class区别

    name 属性用于在 JavaScript 中对元素进行引用,或者在表单提交之后,对表单数据进行引用. html的name和id可以类比身份证的姓名和身份证编号,编号id具有唯一性,一个id只出现一次 ...

  5. 【学习笔记】js中undefined和null的区别和联系

    在JavaScript中存在这样两种原始类型:Null与Undefined.这两种类型常常会使JavaScript的开发人员产生疑惑,在什么时候是Null,什么时候又是Undefined? Undef ...

  6. 机器学习实战 - 读书笔记(13) - 利用PCA来简化数据

    前言 最近在看Peter Harrington写的"机器学习实战",这是我的学习心得,这次是第13章 - 利用PCA来简化数据. 这里介绍,机器学习中的降维技术,可简化样品数据. ...

  7. Spring AOP中定义切点(PointCut)和通知(Advice)

    如果你还不熟悉AOP,请先看AOP基本原理,本文的例子也沿用了AOP基本原理中的例子.切点表达式 切点的功能是指出切面的通知应该从哪里织入应用的执行流.切面只能织入公共方法.在Spring AOP中, ...

  8. Ext.Net学习笔记13:Ext.Net GridPanel Sorter用法

    Ext.Net学习笔记13:Ext.Net GridPanel Sorter用法 这篇笔记将介绍如何使用Ext.Net GridPanel 中使用Sorter. 默认情况下,Ext.Net GridP ...

  9. Spring AOP高级——源码实现(2)Spring AOP中通知器(Advisor)与切面(Aspect)

    本文例子完整源码地址:https://github.com/yu-linfeng/BlogRepositories/tree/master/repositories/Spring%20AOP%E9%A ...

随机推荐

  1. NYOJ 炫舞家st

    #include <iostream>#include <cstring>#include <algorithm>using namespace std; cons ...

  2. Python脚本自动提取和替换代码中的中文

    # -*- coding: utf-8 -*- import os import os.path import re import sys reload(sys) sys.setdefaultenco ...

  3. 我的前端故事----来聊聊react-native应用的健康监控

    监控什么 今天我们来聊聊如何监控你的应用程序,这里的监控说的不是让我们去监控用户,而是监控应用的健康状态,什么是健康状态呢?对于后端的同学来说,在微服务的架构下,每个子服务是否正常工作.返回的结果是否 ...

  4. HTTP请求到爬虫代码的终南捷径

    前阵子在做爬虫的时候学会了各种抓包,看到http请求的时候硬拼代码实在有点累. 后来发现Postman工具是直接可以把Postman请求直接生成对应的代码,这样一下来就美滋滋了. 那么最后的问题就成了 ...

  5. Mego开发文档 - 基本保存操作

    基本保存操作 在Mego中没有更改跟踪,也就是说所有的新增.更新及删除都需要开发者自行判断.Mego会最为实际的将各个数据操作提交给数据库并执行. 添加数据 using (var db = new O ...

  6. ORA-12514:TNS:lisntener does not currently know of service requested in connect descriptor

    在使用工具连接oracle库的时候出现了异常 根据理解初步估计是服务或者监听器没有启动 于是链接到数据库服务器进行查看  服务都已经开启,重启后链接依旧出现上述问题 使用lsnrctl status  ...

  7. Angular 学习笔记 ( CDK - Observers )

    <div class="projected-content-wrapper" (cdkObserveContent)="projectContentChanged( ...

  8. GIT入门笔记(20)- git 开发提交代码过程梳理

    git开发提交流程新项目开发,可以直接往master上提交老项目维护,可以在分支上修改提交,多次add和commit之后,也可以用pull合并主干和本地master,解决冲突后再push 1.检出代码 ...

  9. PHP基础(2)

     测试模板 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  10. Maven的作用是什么

    现在我们开发的项目基本上都是maven项目,maven项目也是一个项目,类似于javaProject,javaWebProject,就是多了些功能. 那就说说究竟多了什么功能呢. 1 . 帮你下载ja ...