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中, ...
随机推荐
- 数值的整数次方(python)
题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. # -*- coding:utf-8 -*- class Solution: ...
- Mac下Chrome浏览器的手机模拟器,开启模拟定位
项目接入百度地图,浏览器调试时需要获取定位信息. 1 打开设置->高级->内容设置->位置,将定位设置为“使用前先询问”,并清空禁止列表. 2 审查元素->Network-&g ...
- 100-days: Three
Title: Singapore(新加坡) set to raise retirement ages as seniors stay healthier be set to do sth. 准备做某 ...
- 21 pythone【入门指南】:string
string是很基础的数据结构,来看看string有哪些常用的操作. #!/bin/python #!---encoding: UTF- s = 'abcdefg' #concat s1 = s + ...
- 【nginx】配置详解
nginx作为web server,是最常用的网络服务器.来看下它的配置文件. 实验1:最基础的配置(可访问静态资源) 包括listen,server_name,root,index,access_l ...
- Intellij Idea创建Android项目
创建工程前请已下载安装好了Intellij Idea和Android SDK. Intellij idea 2016.3.2 步骤 Android SDK设置 在FIle –> Other Se ...
- sqlserver DBA面试题
1.sqlserver 2008 R2 on windows server 2008 R2群集中,有节点A.B,现在需要停机新添加一个节点C进来替换现有节点B,请列出必要的步骤. 2.sqlserve ...
- java 线程Thread 技术--创建线程的方式
在第一节中,对线程的创建我们通过看文档,得知线程的创建有两种方式进行实现,我们进行第一种方式的创建,通过继承Thread 类 ,并且重写它的run 方法,就可以进行线程的创建,所有的程序执行都放在了r ...
- 微信小程序 页面跳转navigator与传递参数
页面之间跳转使用 navigator标签,wx.navigateTo ,wx.redirectTo 1.URL就是跳转的页面路径.上面代码中就是navigator目录下的navigator页面,tit ...
- ubuntu14.04 安装系统/搜狗/QT/qq/wps/CAJviewer
1.安装ubuntu系统 http://jingyan.baidu.com/album/4dc40848491fc5c8d946f1b1.html?picindex=1 官方网站: ht ...