课程链接:

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. HDU3183 贪心/RMQ-ST表

    A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  2. jquery事件一 ---鼠标移入移出

    比较一下几个jquery事件的区别 mouseover() 鼠标进入(进入子元素也触发) mouseout() 鼠标离开(离开子元素也触发) mouseenter() 鼠标进入(进入子元素不触发) m ...

  3. ubuntu安装gnome桌面

    1. apt install gnome-shell 2. apt install ubuntu-gnome-desktop 3. apt install unity-tweak-tool 4. ap ...

  4. 为什么 SQLite 用 C 编写?

    简评:SQLite 官方出品. C是最好的选择 从 2000 年 5 月 29 日开始,SQLite 就选择了 C 语言.直到今天,C 也是实现 SQLite 这样软件库的最佳语言. C语言是实现 S ...

  5. python 绘制抛物线

    %matplotlib inlineimport matplotlib.pyplot as plt import numpy as npx = range(100) y = [val**2 for v ...

  6. nginx高性能WEB服务器系列之五--实战项目线上nginx多站点配置

    nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...

  7. 解决NTFS文件系统下的文件/文件夹属性中没有安全选项卡的问题

    注册表项: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer 键:NoSecurityTab ...

  8. [转] 基于Gitlab CI搭建持续集成环境

    [From] https://blog.csdn.net/wGL3k77y9fR1k61T1aS/article/details/78798577 前言 本文是在12月12号迅雷@赵兵在前端早读课第三 ...

  9. linux下 git使用小记下

    之前都是自己windows做脚本,完成文件的备份和管理,第一次使用git  转发一个博主很有用的笔记 ,并在此做了一点补充学习了 引用:https://www.cnblogs.com/chenfuli ...

  10. cookie session token详解

    cookie session token详解 转自:http://www.cnblogs.com/moyand/ 发展史 1.很久很久以前,Web 基本上就是文档的浏览而已, 既然是浏览,作为服务器, ...