课程链接:

1    解析

1.1  通知执行顺序

2    代码演练

1    解析

1.1  通知执行顺序

aop执行方式为:前置通知==>所要增强的方法==>后置通知==>最终通知

在出现异常时会进行:前置通知==>所要增强的方法==>异常通知==>最终通知

而用xml进行配置时,是按照我们写好的顺序进行动态组合完成,最终和后置通知是随着xml配置的前后顺序改变的,但是经过测试不会影响前置和所要增强的方法的顺序,但是会影响最终和后置通知的位置.

我认为利用环绕通知进行方法的增强(aop:around)是一个比较好的方式,不会出现顺序问题.

2    代码演练

2.1  前置通知和后置通知

业务类:

package com.imooc.aop.schema.advice.biz;

public class AspectBiz {

    public void biz(){
System.out.println("MoocAspect biz");
} public void biz2(){
System.out.println("MoocAspect biz2");
} }

配置文件:

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <bean id = "moocAspect" class = "com.imooc.aop.schema.advice.MoocAspect"></bean>
<bean id = "AspectBiz" class = "com.imooc.aop.schema.advice.biz.AspectBiz"></bean> <aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<!-- 声明切入点:从哪里开始执行 -->
<!-- 执行com.imooc.aop.schema.advice.biz包下的所有Biz结尾的java类中的所有方法时 -->
<aop:pointcut expression="execution(* com.imooc.aop.schema.advice.biz.*Biz.*(..))" id="moocPointCut"/>
<aop:before method="before" pointcut-ref="moocPointCut"/>
<aop:after-returning method="after" pointcut-ref="moocPointCut"/>

</aop:aspect>
</aop:config> </beans>

通知(名词)所在的类:

package com.imooc.aop.schema.advice;

public class MoocAspect {

    public void before(){
System.out.println("before");
} public void after(){
System.out.println("after"
);
}
}

测试类:

package com.imooc.test.aop;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner; import com.imooc.aop.schema.advice.biz.AspectBiz;
import com.imooc.test.base.UnitTestBase; @RunWith(BlockJUnit4ClassRunner.class)
public class TestAOPSchemaAdvice extends UnitTestBase { public TestAOPSchemaAdvice(){
super("classpath:spring-aop-schema-advice.xml");
} @Test
public void testBiz(){
AspectBiz aBiz = super.getbean("AspectBiz");
aBiz.biz();
aBiz.biz2();

} }

打印结果:

四月 15, 2019 8:19:44 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@285855bd: startup date [Mon Apr 15 20:19:44 CST 2019]; root of context hierarchy
四月 15, 2019 8:19:44 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-aop-schema-advice.xml]
before
MoocAspect biz
after
before
MoocAspect biz2
after

四月 15, 2019 8:19:45 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@285855bd: startup date [Mon Apr 15 20:19:44 CST 2019]; root of context hierarchy

 2.2  异常通知和最终通知

业务类:

package com.imooc.aop.schema.advice.biz;

public class AspectBiz {

    public void biz(){
System.out.println("MoocAspect biz");
throw new RuntimeException();
} }

配置文件:

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <bean id = "moocAspect" class = "com.imooc.aop.schema.advice.MoocAspect"></bean>
<bean id = "AspectBiz" class = "com.imooc.aop.schema.advice.biz.AspectBiz"></bean> <aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<!-- 声明切入点:从哪里开始执行 -->
<!-- 执行com.imooc.aop.schema.advice.biz包下的所有Biz结尾的java类中的所有方法时 -->
<aop:pointcut expression="execution(* com.imooc.aop.schema.advice.biz.*Biz.*(..))" id="moocPointCut"/>
<aop:before method="before" pointcut-ref="moocPointCut"/>
<aop:after-returning method="afterreturning" pointcut-ref="moocPointCut"/>
<aop:after-throwing method="throwing" pointcut-ref="moocPointCut"/>
<aop:after
method="after" pointcut-ref="moocPointCut"/>
    </aop:aspect>
</aop:config> </beans>

通知所在类:

package com.imooc.aop.schema.advice;

public class MoocAspect {

    public void before(){
System.out.println("before");
} public void afterreturning(){
System.out.println("after returning");
} public void throwing(){
System.out.println("throw");
} public void after(){
System.out.println("Say Love me");
}
}

测试类:

package com.imooc.test.aop;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner; import com.imooc.aop.schema.advice.biz.AspectBiz;
import com.imooc.test.base.UnitTestBase; @RunWith(BlockJUnit4ClassRunner.class)
public class TestAOPSchemaAdvice extends UnitTestBase { public TestAOPSchemaAdvice(){
super("classpath:spring-aop-schema-advice.xml");
} @Test
public void testBiz(){
AspectBiz aBiz = super.getbean("AspectBiz");
aBiz.biz();
} }

打印日志:

四月 16, 2019 7:18:45 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4b19b8ae: startup date [Tue Apr 16 07:18:45 CST 2019]; root of context hierarchy
四月 16, 2019 7:18:45 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-aop-schema-advice.xml]
before
MoocAspect biz
throw
Say Love me

四月 16, 2019 7:18:46 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@4b19b8ae: startup date [Tue Apr 16 07:18:45 CST 2019]; root of context hierarchy

Spring课程 Spring入门篇 5-4 advice应用(上)的更多相关文章

  1. Spring Boot -01- 快速入门篇(图文教程)

    Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...

  2. Spring实践系列-入门篇(一)

    本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...

  3. VT 入门篇——最小 VT 实现(上)

    写在前面   此系列是本人一个字一个字码出来的,包括示例和实验截图.由于系统内核的复杂性,故可能有错误或者不全面的地方,如有错误,欢迎批评指正,本教程将会长期更新. 如有好的建议,欢迎反馈.码字不易, ...

  4. Spring课程 Spring入门篇 7-3 advice扩展

    课程链接: 1 解析 1.1 advice中aspect 切面传参 1.2 通知参数名称——argNames属性, 参数为 JoinPoint.ProceedingJoinPoint.JoinPoin ...

  5. Spring课程 Spring入门篇 7-2 Advice定义及实例

    1 解析 1.1 通知:after和afterreturning的区别 1.2 @RunWith 是什么? 2 代码演练 2.1 注解方式配置通知的两种方式 2.2 异常通知 2.3 非异常通知 1 ...

  6. Spring课程 Spring入门篇 6-1 Spring AOP API的PointCut、advice的概念及应用

    本节主要是模拟spring aop 的过程. 实现spring aop的过程 这一节老师虽然说是以后在工作中不常用这些api,实际上了解还是有好处的, 我们可以从中模拟一下spring aop的过程. ...

  7. Spring课程 Spring入门篇 5-5 advice应用(下)

    2 代码演练 2.1 环绕通知(不带参数) 2.2 环绕通知(带参数) 2 代码演练 2.1 环绕通知(不带参数) 实体类: package com.imooc.aop.schema.advice.b ...

  8. Spring课程 Spring入门篇 6-2 ProxyFactoryBean及相关内容(上)

    1 解析 1.1 类的方式实现各种通知需要实现的接口 1.2 创建Spring aop代理的优点及方法 1.3 代理控制切入点和通知的顺序的代码实现(具体完全实现,见代码2.1) 1.4 代理方式选择 ...

  9. Spring课程 Spring入门篇 6-3 ProxyFactoryBean及相关内容(下)

    1 解析 1.1 使用global advisors demo 1.2 jdk代理和cglib代理的选择 1.3 如何强制使用CGLIB实现AOP? 1.4 JDK动态代理和CGLIB字节码生成的区别 ...

随机推荐

  1. CentOS 下安装 Hexo 博客

    前言: 之前一直使用Wordpress架构作为博客网站架构,但是现在频频爆出漏洞.实在是不敢用也不想用了,然后群里面有小伙伴用的是 Github 搭建 Hexo 架构博客.就写了这个教程,给自己做个参 ...

  2. CF765F Souvenirs 离线+线段树+主席树

    $ \color{#0066ff}{ 题目描述 }$ A县旁,连绵着一条长度为 n 的山脉,这条山脉由 n 座山峰组成,第 i 座山 峰的高度为 ai.作为著名的旅游县城,每天来到山脉游玩的旅客络绎不 ...

  3. mybatis的入门(二)

    上篇文章首先介绍了JDBC操作数据库的相关知识,并总结了JDBC操作数据存在的一些问题,那么这篇文章我们来介绍下mybatis的相关内容 一.mybatis的架构图 下面对上面的架构图详细说明下: 1 ...

  4. Angular material mat-icon 资源参考_File

    ul,li>ol { margin-bottom: 0 } dt { font-weight: 700 } dd { margin: 0 1.5em 1.5em } img { height: ...

  5. [SCOI2018]游泳池(计算几何+分数规划+最大权闭合子图)

    题目链接 https://www.luogu.org/problemnew/show/U56187 注:题面参考了网上的其他博客,并非原题题面,因此数据范围可能有误.数据为原创数据. 题解 其实就是许 ...

  6. SPOJ - COT 路径构造主席树

    题意:给出一个带权树,多次询问路径\((u,v)\)的第k小权值 这是主席树往区间扩展到树上的套路题 由于是按路径查询,我们无法使用dfs序,但可利用主席树对父亲扩展的方法构造出链 因此要用dfs构造 ...

  7. Git修改文件

    如果我们修改了本地的某个文件但是没有提交,这时我们用 $ git status可以看到提示,例如我在readme2.txt里面新加了一行,然后查看状态: git status命令可以让我们时刻掌握仓库 ...

  8. 如何获取用户的地理位置? && html5 地理位置

    推荐网站 https://html5demos.com/geo/ 我们有时候可能希望首先获得用户的地理位置,然后根据不同的地理位置(更具针对性地)推送不同的信息等等. 下面这段代码就可以在你有jQue ...

  9. c++ 网络编程(三) LINUX/windows 进程间的通信原理与实现代码 基于多进程的服务端实现

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9613027.html 锲子:进程与线程是什么,他们的区别在哪里: 1 进程概念 进程是程序的一 ...

  10. java实现任务调度

    最近的一个小项目是做一个简单的数据仓库,需要将其他数据库的数据抽取出来,并通过而出抽取成页面需要的数据,以空间换时间的方式,让后端报表查询更快. 因为在抽取的过程中,有一定的先后顺序,需要做一个任务调 ...