AOP 环绕通知 (Schema-base方式) 和 AspectJ方式在通知中获取切点的参数
环绕通知(Schema- base方式)
1、把前置通知和后置通知都写到一个通知中,组成了环绕通知
2、实现步骤:
2.1 新建一个类实现 MethodInterceptor 接口
public class MyArround implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.println("环绕--前置通知11111");
Object result = arg0.proceed();//放行,调用切点方式
System.out.println("环绕--后置通知222");
return result;
}
}
2.2 配置applicationContext.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"
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.xsd"> <bean id="myarround" class="com.bjsxt.advice.MyArround"></bean>
<bean id="demo" class="com.bjsxt.test.Demo"></bean>
<aop:config>
<aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1())" id="mypoint"/>
<aop:advisor advice-ref="myarround" pointcut-ref="mypoint"/>
</aop:config> </beans>
通过AspectJ方式获取在通知中获取切点的参数
1、新建类
public class Advice {
public void mybefore(String name1,int age1){
System.out.println("前置通知"+name1+" "+age1);
}
public void myafter(){
System.out.println("后置通知1");
}
public void myafterint(){
System.out.println("后置通知222");
}
public void mythrow(){
System.out.println("触发异常");
}
}
2、在spring中配置ApplicationContext.xml
2.1、<aop:after/>:后置通知,不管切点是否出现异常,都执行
2.2、<aop:after-returning/>:后置通知,切点出现异常,就不会执行
2.3、<aop:after/> 和<aop:returnint> 和<aop:after-throwing> 的执行顺序和 配置顺序有关
2.4、不能使用 &&
2.5、args(名称) 名称自定义,但是顺序和demo1(参数,参数) 对应
2.6、<aop:before> 中 arg-names=" 名称 " 名称来源于execution中args
<?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.xsd"> <bean id="demo" class="com.bjsxt.test.Demo"></bean>
<bean id="advice" class="com.bjsxt.advice.Advice"></bean> <aop:config>
<aop:aspect ref="advice"> args 中有几个参数 下面的arg-names就要有几个参数
<aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1(String,int)) and args(name1,age1)" id="mypoint"/>
<aop:before method="mybefore" pointcut-ref="mypoint" arg-names="name1,age1" /> arg-names中的参数名,要和通知类中的方式的参数名一致
<!-- <aop:after method="myafter" pointcut-ref="mypoint"/>
<aop:after-returning method="myafterint" pointcut-ref="mypoint"/>
<aop:after-throwing method="mythrow" pointcut-ref="mypoint"/> -->
</aop:aspect>
</aop:config> </beans>
AOP 环绕通知 (Schema-base方式) 和 AspectJ方式在通知中获取切点的参数的更多相关文章
- beego中获取url以及参数的方式
以下都全默认在controller下执行 获取当前请求的referer fmt.Println(this.Ctx.Request.Referer()) 输出:http://localhost:8080 ...
- Spring中关于AOP的实践之AspectJ方式实现通知
(本文中如有不当之处,恳请批评指正) AspectJ方式的简化了通知的出现复杂度.但是对配置文件的操作复杂度有了一定的提升 一. 配置通知 package com.xkx.adviceDemo; im ...
- :Spring-06 -AOP [面向切面编程] -配置异常通知的两种方式--AspectJ 方式 -Schema-based 方式
三.配置异常通知的步骤(AspectJ 方式) 1.只有当切点报异常才能触发异常通知 2.在spring 中有AspectJ 方式提供了异常通知的办法 3.实现步骤: 3.1新建类,在类写任意名称的方 ...
- Spring学习4-面向切面(AOP)之schema配置方式
一.通过Scheme配置实现AOP步骤(Spring AOP环境的环境与上篇博文 Spring接口方式相同) 步骤一.编写业务类: public class AspectBusiness { ...
- spring aop环绕通知
[Spring实战]—— 9 AOP环绕通知 假如有这么一个场景,需要统计某个方法执行的时间,如何做呢? 典型的会想到在方法执行前记录时间,方法执行后再次记录,得出运行的时间. 如果采用Sprin ...
- AOP 环绕通知 集成了前置 后置 返回通知等功能
AOP 环绕通知 集成了前置 后置 返回通知等功能
- 5.2 spring5源码--spring AOP源码分析二--切面的配置方式
目标: 1. 什么是AOP, 什么是AspectJ 2. 什么是Spring AOP 3. Spring AOP注解版实现原理 4. Spring AOP切面原理解析 一. 认识AOP及其使用 详见博 ...
- Spring(二十):Spring AOP(四):基于配置文件的方式来配置 AOP
基于配置文件的方式来配置 AOP 前边三个章节<Spring(十七):Spring AOP(一):简介>.<Spring(十八):Spring AOP(二):通知(前置.后置.返回. ...
- Spring AOP中定义切点(PointCut)和通知(Advice)
如果你还不熟悉AOP,请先看AOP基本原理,本文的例子也沿用了AOP基本原理中的例子.切点表达式 切点的功能是指出切面的通知应该从哪里织入应用的执行流.切面只能织入公共方法.在Spring AOP中, ...
随机推荐
- poj 1170状压dp
题目链接:https://vjudge.net/problem/POJ-1170 题意:输入n,表示有那种物品,接下来n行,每行a,b,c三个变量,a表示物品种类,b是物品数量,c代表物品的单价.接下 ...
- 【转】 UI自动化测试的关注点
我发现了,大家极度关心自动化测试,尤其是UI自动化测试,虽然现在作为专项测试,离开这些越来越远了,但总能遥想以前,我总能想起自己做nokia的WindowsLive的ui自动化,做web的自动化测试, ...
- H5入门
1.基本骨架 <!DOCTYPE html> <html> <head><title>标题</title><meta charset= ...
- js对键盘输入事件绑定到特定按钮
转自:https://www.cnblogs.com/liluping860122/archive/2013/05/25/3099103.html<script type="text/ ...
- [Codeforces_713A]Sonya and Queries
题目链接 http://codeforces.com/problemset/problem/713/A 题意 三种操作: + ai 集合里加一个整数ai,相同数记为多个. - ai 集合里减一个 ...
- python中类变量和成员变量、局部变量总结
class Member(): num= #类变量,可以直接用类调用,或用实例对象调用 def __init__(self,x,y): self.x=x #实例变量(成员变量),需要它是在类的构造函数 ...
- AngularJS——第3章 指令
第3章 指令 所谓指令就是AngularJS自定义的HTML属性或标签,这些指令都是以ng-做为前缀的,例如ng-app.ng-controller.ng-repeat等. 3.1 内置指令 ng-a ...
- composer 安装新包失败的原因之一
各种方法都尝试过了,然而最大的可能就是不能访问国外的资源! 1.使用vpnFQ下载 2.修改一下composer的配置,命令如下: composer config -g repo.packagist ...
- js文件,同样的路径,拷贝过来的为什么不能访问
从解决方案管理器中拖过来的可以直接访问,而从 bundleconfig中拷贝过来后修改的就访问不到. 如下: 引用一: <script src="~/Content/Plugins/j ...
- stl中顺序性容器,关联容器两者粗略解释
什么是容器 首先,我们必须理解一下什么是容器,在C++ 中容器被定义为:在数据存储上,有一种对象类型,它可以持有其它对象或指向其它对像的指针,这种对象类型就叫做容器.很简单,容器就是保存其它对象的对象 ...