点击下载本示例相关代码

关于spring aop的具体使用,暂时不在这里讲解,后面会特定来讲解,本次主要探讨spring中通知执行的顺序。

spring中通知分为以下几种:

  1. before:前置通知,在方法执行之前执行
  2. around:环绕通知,在方法执行前后执行
  3. afterreturning:在方法执行成功后执行
  4. afterthrowning:异常通知,在方法发生异常后执行
  5. after:后置通知,在方法执行之后执行(不管方法执行成功还是失败)

下面我们将每种通知创建两个,代码如下:
1、before通知2个
package com.cn.aop.demo4;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; /**
* 我的技术网站 2016/11/17 0017.
*/
@Component
@Aspect
@Order(100000)
public class Before1 {
@Before(value = "execution(* com.cn.aop.demo4.*Impl.*(..))")
public void exec(){
Order order = this.getClass().getAnnotation(Order.class);
System.out.println(Before1.class+" Before通知 order = "+order.value());
}
}
package com.cn.aop.demo4;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; /**
* 我的技术网站 2016/11/17 0017.
*/
@Component
@Aspect
@Order(100000)
public class Before2 {
@Before(value = "execution(* com.cn.aop.demo4.*Impl.*(..))")
public void exec(){
Order order = this.getClass().getAnnotation(Order.class);
System.out.println(Before2.class+" Before通知 order = "+order.value());
}
}

2、around通知2个

package com.cn.aop.demo4;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; /**
* 我的技术网站 2016/11/17 0017.
*/
@Component
@Aspect
@Order(100000)
public class Around1 {
@Around(value = "execution(* com.cn.aop.demo4.*Impl.*(..))")
public Object exec(ProceedingJoinPoint invocation) throws Throwable {
Order order = this.getClass().getAnnotation(Order.class);
System.out.println(Around1.class+" Around通知 order = "+order.value()+" start");
Object result = invocation.proceed();
System.out.println(Around1.class+" Around通知 order = "+order.value()+" end");
return result;
}
}
package com.cn.aop.demo4;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; /**
* 我的技术网站 2016/11/17 0017.
*/
@Component
@Aspect
@Order(100000)
public class Around2 {
@Around(value = "execution(* com.cn.aop.demo4.*Impl.*(..))")
public Object exec(ProceedingJoinPoint invocation) throws Throwable {
Order order = this.getClass().getAnnotation(Order.class);
System.out.println(Around2.class+" Around通知 order = "+order.value()+" start");
Object result = invocation.proceed();
System.out.println(Around2.class+" Around通知 order = "+order.value()+" end");
return result;
}
}

3、afterreturning通知2个

package com.cn.aop.demo4;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; /**
* 我的技术网站 2016/11/17 0017.
*/
@Component
@Aspect
@Order(100000)
public class AfterReturning1 {
@AfterReturning(value = "execution(* com.cn.aop.demo4.*Impl.*(..))")
public void exec(){
Order order = this.getClass().getAnnotation(Order.class);
System.out.println(AfterReturning1.class+" AfterReturning通知 order = "+order.value());
}
}
package com.cn.aop.demo4;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; /**
* 我的技术网站 2016/11/17 0017.
*/
@Component
@Aspect
@Order(100000)
public class AfterReturning2 {
@AfterReturning(value = "execution(* com.cn.aop.demo4.*Impl.*(..))")
public void exec(){
Order order = this.getClass().getAnnotation(Order.class);
System.out.println(AfterReturning2.class+" AfterReturning通知 order = "+order.value());
}
}

4、afterthrowning通知2个

package com.cn.aop.demo4;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; /**
* 我的技术网站 2016/11/17 0017.
*/
@Component
@Aspect
@Order(100000)
public class AfterThrowing1 {
@AfterThrowing(value = "execution(* com.cn.aop.demo4.*Impl.*(..))")
public void exec(){
Order order = this.getClass().getAnnotation(Order.class);
System.out.println(AfterThrowing1.class+" AfterThrowing通知 order = "+order.value());
}
}
package com.cn.aop.demo4;

import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; /**
* 我的技术网站 2016/11/17 0017.
*/
@Component
@Aspect
@Order(100000)
public class AfterThrowing2 {
@AfterThrowing(value = "execution(* com.cn.aop.demo4.*Impl.*(..))")
public void exec(){
Order order = this.getClass().getAnnotation(Order.class);
System.out.println(AfterThrowing2.class+" AfterThrowing通知 order = "+order.value());
}
}

5、after通知两个

package com.cn.aop.demo4;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; /**
* 我的技术网站 2016/11/17 0017.
*/
@Component
@Aspect
@Order
public class After1 {
@After(value = "execution(* com.cn.aop.demo4.*Impl.*(..))")
public void exec(){
Order order = this.getClass().getAnnotation(Order.class);
System.out.println(After1.class+" After通知 order = "+order.value());
}
}
package com.cn.aop.demo4;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; /**
* 我的技术网站 2016/11/17 0017.
*/
@Component
@Aspect
@Order(100000)
public class After2 {
@After(value = "execution(* com.cn.aop.demo4.*Impl.*(..))")
public void exec(){
Order order = this.getClass().getAnnotation(Order.class);
System.out.println(After2.class+" After通知 order = "+order.value());
}
}

6、其他文件

  • 声明1个接口和一个实现类,在本实现类上面添加上面10个通知
package com.cn.aop.demo4;

/**
* 我的技术网站 2016/11/15 0015.
*/
public interface IAService {
String m1();
void m2();
}
package com.cn.aop.demo4;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
* 我的技术网站 2016/11/15 0015.
*/
@Component("aservice")
public class AServiceImpl implements IAService { public String m1()
{
System.out.println(AServiceImpl.class+".m1()");
return AServiceImpl.class+".m1()";
} @Override
public void m2() {
int i=0;
System.out.println(10/i);
}
}
  • spring配置文件spring-demo4.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" default-autowire="byName">
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.cn.aop.demo4"/>
</beans>

  • 测试类
package com.cn.aop.demo4;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* 我的技术网站 2016/11/17 0017.
*/
public class Client {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/aop/spring-demo4.xml");
IAService aservice = context.getBean("aservice", IAService.class);
System.out.println("\n========================\n");
aservice.m1();
System.out.println("\n========================\n");
try {
aservice.m2();
} catch (Exception e){
//暂时不输出错误信息
}
}
}

1、上面所有拦截器的order都为100000,执行结果如下

========================

class com.cn.aop.demo4.Around1 Around通知 order = 100000 start
class com.cn.aop.demo4.Around2 Around通知 order = 100000 start
class com.cn.aop.demo4.Before1 Before通知 order = 100000
class com.cn.aop.demo4.Before2 Before通知 order = 100000
class com.cn.aop.demo4.AServiceImpl.m1()
class com.cn.aop.demo4.Around2 Around通知 order = 100000 end
class com.cn.aop.demo4.Around1 Around通知 order = 100000 end
class com.cn.aop.demo4.AfterReturning1 AfterReturning通知 order = 100000
class com.cn.aop.demo4.AfterReturning2 AfterReturning通知 order = 100000
class com.cn.aop.demo4.After2 After通知 order = 100000
class com.cn.aop.demo4.After1 After通知 order = 100000 ======================== class com.cn.aop.demo4.Around1 Around通知 order = 100000 start
class com.cn.aop.demo4.Around2 Around通知 order = 100000 start
class com.cn.aop.demo4.Before1 Before通知 order = 100000
class com.cn.aop.demo4.Before2 Before通知 order = 100000
class com.cn.aop.demo4.AfterThrowing2 AfterThrowing通知 order = 100000
class com.cn.aop.demo4.AfterThrowing1 AfterThrowing通知 order = 100000
class com.cn.aop.demo4.After2 After通知 order = 100000
class com.cn.aop.demo4.After1 After通知 order = 100000

2、修改配置

Before1 @Order(1)
Before2 @Order(2)

client执行结果

========================

class com.cn.aop.demo4.Before1 Before通知 order = 1
class com.cn.aop.demo4.Before2 Before通知 order = 2
class com.cn.aop.demo4.Around1 Around通知 order = 100000 start
class com.cn.aop.demo4.Around2 Around通知 order = 100000 start
class com.cn.aop.demo4.AServiceImpl.m1()
class com.cn.aop.demo4.Around2 Around通知 order = 100000 end
class com.cn.aop.demo4.Around1 Around通知 order = 100000 end
class com.cn.aop.demo4.AfterReturning1 AfterReturning通知 order = 100000
class com.cn.aop.demo4.AfterReturning2 AfterReturning通知 order = 100000
class com.cn.aop.demo4.After2 After通知 order = 100000
class com.cn.aop.demo4.After1 After通知 order = 100000 ======================== class com.cn.aop.demo4.Before1 Before通知 order = 1
class com.cn.aop.demo4.Before2 Before通知 order = 2
class com.cn.aop.demo4.Around1 Around通知 order = 100000 start
class com.cn.aop.demo4.Around2 Around通知 order = 100000 start
class com.cn.aop.demo4.AfterThrowing2 AfterThrowing通知 order = 100000
class com.cn.aop.demo4.AfterThrowing1 AfterThrowing通知 order = 100000
class com.cn.aop.demo4.After2 After通知 order = 100000
class com.cn.aop.demo4.After1 After通知 order = 100000

3、修改配置
Before1 @Order(1)
Before2 @Order(2)
AfterReturning1 @Order(1)
AfterReturning2 @Order(1000001)
AfterThrowing1 @Order(1)
AfterThrowing2 @Order(1000001)
After1 @Order(1)
After2 @Order(1000001)

执行结果:

========================

class com.cn.aop.demo4.Before1 Before通知 order = 1
class com.cn.aop.demo4.Before2 Before通知 order = 2
class com.cn.aop.demo4.Around1 Around通知 order = 100000 start
class com.cn.aop.demo4.Around2 Around通知 order = 100000 start
class com.cn.aop.demo4.AServiceImpl.m1()
class com.cn.aop.demo4.AfterReturning2 AfterReturning通知 order = 1000001
class com.cn.aop.demo4.After2 After通知 order = 1000001
class com.cn.aop.demo4.Around2 Around通知 order = 100000 end
class com.cn.aop.demo4.Around1 Around通知 order = 100000 end
class com.cn.aop.demo4.AfterReturning1 AfterReturning通知 order = 1
class com.cn.aop.demo4.After1 After通知 order = 1 ======================== class com.cn.aop.demo4.Before1 Before通知 order = 1
class com.cn.aop.demo4.Before2 Before通知 order = 2
class com.cn.aop.demo4.Around1 Around通知 order = 100000 start
class com.cn.aop.demo4.Around2 Around通知 order = 100000 start
class com.cn.aop.demo4.AfterThrowing2 AfterThrowing通知 order = 1000001
class com.cn.aop.demo4.After2 After通知 order = 1000001
class com.cn.aop.demo4.AfterThrowing1 AfterThrowing通知 order = 1
class com.cn.aop.demo4.After1 After通知 order = 1

结论:

1、无异常情况
  • 所有通知order一样,执行顺序:around start -> before ->around
    start -> afterreturning -> after
  • before.order < around.order,执行顺序: before -> around start
  • afterreturning.order > around.order,执行顺序:afterreturning -> around
    end
  • after.order > around.order,执行顺序:after -> around
    end
  • after.order >afterreturning.order,执行顺序
    after -> afterreturning
2、异常情况
  • 所有通知order一样,执行顺序:around start -> before ->  afterthrowing -> after
  • before.order < around.order,执行顺序: before -> around start
  • after.order > afterthrowing .order,执行顺序:after -> afterthrowing

如果喜欢,点个赞,关注公众号,我们将最好的内容分享与你!谢谢!




spring通知执行的顺序的更多相关文章

  1. Spring切面通知执行的顺序(Advice Order)

    问题描述 如果在Spring的程序中同时定义了环绕通知(Around)和前置通知(Before)..那么,有以下问题: 1.怎么让两个切面通知都起作用 2.或者让两者切面按自己指定的顺序进行执行? 3 ...

  2. 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~

    简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...

  3. spring多个AOP执行先后顺序(面试问题:怎么控制多个aop的执行循序)

    转载:spring多个AOP执行先后顺序(面试问题:怎么控制多个aop的执行循序) 众所周知,spring声明式事务是基于AOP实现的,那么,如果我们在同一个方法自定义多个AOP,我们如何指定他们的执 ...

  4. 【原】不定义Order属性,通过切面类的定义顺序来决定通知执行的先后顺序

    [结论] 在多个切面类的“切入点相同”并且每个切面都“没有定义order属性”的情况下,则切面类(中的通知)的执行顺序与该切面类在<aop:config>元素中“声明的顺序”相关,即先声明 ...

  5. 【原】Order属性决定了不同切面类中通知执行的先后顺序

    [障碍再现] MyBatis配置多数据源时,数据源切换失败. [原因分析]    自定义切面和Spring自带事务切面“即<aop:advisor>”执行的先后顺序导致数据源不能切换成功. ...

  6. Spring通知类型及使用ProxyFactoryBean创建AOP代理

    Spring 通知类型 通过前面的学习可以知道,通知(Advice)其实就是对目标切入点进行增强的内容,Spring AOP 为通知(Advice)提供了 org.aopalliance.aop.Ad ...

  7. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring通知类型及使用ProxyFactoryBean创建AOP代理

    通知(Advice)其实就是对目标切入点进行增强的内容,Spring AOP 为通知(Advice)提供了 org.aopalliance.aop.Advice 接口. Spring 通知按照在目标类 ...

  8. 使用@AutoConfigureBefore、After、Order调整Spring Boot自动配置顺序

    前言 Spring Boot是Spring家族具有划时代意义的一款产品,它发展自Spring Framework却又高于它,这种高于主要表现在其最重要的三大特性,而相较于这三大特性中更为重要的便是Sp ...

  9. Java中的继承与静态static等的执行先后顺序

    package extend; public class X { Y y=new Y(); static{  System.out.println("tttt"); } X(){  ...

随机推荐

  1. 基于SpringBoot从零构建博客网站 - 整合lombok和mybatis-plus提高开发效率

    在上一章节中<技术选型和整合开发环境>,确定了开发的技术,但是如果直接这样用的话,可能开发效率会不高,为了提高开发的效率,这里再整合lombok和mybatis-plus两个组件. 1.l ...

  2. 微服务架构 - 搭建docker本地镜像仓库并提供权限校验及UI界面

    搭建docker本地镜像仓库并提供权限校验及UI界面 docker本地镜像仓库的作用跟maven私服差不多,特别是公司级或者是小组级开发好的docker仓库可以上传到本地镜像仓库中,需要用时,直接从本 ...

  3. 【JVM虚拟机】(5)---深入理解JVM-Class中常量池

    深入理解Class---常量池 一.概念 1.jvm生命周期 启动:当启动一个java程序时,一个jvm实例就诞生了,任何一个拥有main方法的class都可以作为jvm实例运行的起点. 运行:mai ...

  4. 前端笔记之ES678&Webpack&Babel(中)对象|字符串|数组的扩展&函数新特性&类

    一.对象的扩展 1.1对象属性名表达式 ES6可以在JSON中使用[]包裹一个key的名字.此时这个key将用表达式作为属性名(被当做变量求值),这个key值必须是字符串. var a = 'name ...

  5. c#根据路径(url)下载图片

    方法一:根据路径下载图片 1 /// <summary> /// 图片另存为 /// </summary> /// <param name="url" ...

  6. Java KeyStore 用命令生成keystore文件

    1.生成keyStore文件 在命令行下执行以下命令: Shell代码 收藏代码 keytool -genkey -validity 36000 -alias www.zlex.org -keyalg ...

  7. Api管家系列(一):初探

    前段时间发现一个很好用的API管理工具--API管家,用了一段时间,已经感觉离不开了,抱着分享使我快乐的想法,因为刚开始用的时候随便写过一篇简介,不是很详细,所以现在就重新写,把我这段时间使用的经验和 ...

  8. 通过 mysqlbinlog 和 grep 命令定位binlog文件中指定操作

    1.binlog日志基本知识 MySQL的二进制日志binlog可以说是MySQL最重要的日志,它记录了所有的DDL和DML语句(除了数据查询语句select),以事件形式记录,还包含语句所执行的消耗 ...

  9. Java关于读取Excel文件~xlsx xls csv txt 格式文件~持续汇总~

    所需的jar百度网盘链接:https://pan.baidu.com/s/146mrCImkZVvi1CJ5KoiEhQ提取码:c329 1 需要导入jar包,缺1不可 dom4j-1.6.1.jar ...

  10. SqlServer 递归查询

    --查询部门及下属部门列表 WITH TEMP --递归 AS (SELECT Id, Code, Name, ParentId FROM [dbo].[AspSysDepartments] --查询 ...