环绕通知(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方式在通知中获取切点的参数的更多相关文章

  1. beego中获取url以及参数的方式

    以下都全默认在controller下执行 获取当前请求的referer fmt.Println(this.Ctx.Request.Referer()) 输出:http://localhost:8080 ...

  2. Spring中关于AOP的实践之AspectJ方式实现通知

    (本文中如有不当之处,恳请批评指正) AspectJ方式的简化了通知的出现复杂度.但是对配置文件的操作复杂度有了一定的提升 一. 配置通知 package com.xkx.adviceDemo; im ...

  3. :Spring-06 -AOP [面向切面编程] -配置异常通知的两种方式--AspectJ 方式 -Schema-based 方式

    三.配置异常通知的步骤(AspectJ 方式) 1.只有当切点报异常才能触发异常通知 2.在spring 中有AspectJ 方式提供了异常通知的办法 3.实现步骤: 3.1新建类,在类写任意名称的方 ...

  4. Spring学习4-面向切面(AOP)之schema配置方式

    一.通过Scheme配置实现AOP步骤(Spring AOP环境的环境与上篇博文 Spring接口方式相同)    步骤一.编写业务类: public class AspectBusiness {   ...

  5. spring aop环绕通知

    [Spring实战]—— 9 AOP环绕通知   假如有这么一个场景,需要统计某个方法执行的时间,如何做呢? 典型的会想到在方法执行前记录时间,方法执行后再次记录,得出运行的时间. 如果采用Sprin ...

  6. AOP 环绕通知 集成了前置 后置 返回通知等功能

    AOP 环绕通知 集成了前置 后置 返回通知等功能

  7. 5.2 spring5源码--spring AOP源码分析二--切面的配置方式

    目标: 1. 什么是AOP, 什么是AspectJ 2. 什么是Spring AOP 3. Spring AOP注解版实现原理 4. Spring AOP切面原理解析 一. 认识AOP及其使用 详见博 ...

  8. Spring(二十):Spring AOP(四):基于配置文件的方式来配置 AOP

    基于配置文件的方式来配置 AOP 前边三个章节<Spring(十七):Spring AOP(一):简介>.<Spring(十八):Spring AOP(二):通知(前置.后置.返回. ...

  9. Spring AOP中定义切点(PointCut)和通知(Advice)

    如果你还不熟悉AOP,请先看AOP基本原理,本文的例子也沿用了AOP基本原理中的例子.切点表达式 切点的功能是指出切面的通知应该从哪里织入应用的执行流.切面只能织入公共方法.在Spring AOP中, ...

随机推荐

  1. springboot找不到主类

    在学习springboot的时候,前几天写了一个demo,正常运行,一点问题也没有.今天运行不起来了. 报错:找不到主类 解决方案: Project->Clean->选中项目,点击Clea ...

  2. 二叉树,B树,B+树,红黑树 简介

    什么是二叉树? 在计算机科学中,二叉树是每个节点最多有两个子树的树结构.通常子树被称作“左子树”和“右子树”,左子树和右子树同时也是二叉树.二叉树的子树有左右之分,并且次序不能任意颠倒.二叉树是递归定 ...

  3. JS 解决json字符串转换成json树形输出

    问题: 后台获取一个字符串,格式为  string +jsonList+string+..... 就是传过来一堆数据,然后其中包含了一个json格式的list, 我们希望能将它输出成树形结构显示,能够 ...

  4. 上海高校金马五校赛 F题:1 + 2 = 3?

    链接:https://www.nowcoder.com/acm/contest/91/F来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言26214 ...

  5. 7. Reverse Integer (整数的溢出)

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 For the p ...

  6. 今天清理C盘空间,发现Unity的一个问题

    C:\Users\Acer\AppData\LocalLow\Unity\Caches\GiCache AppData目录下,Unity占用了大量C盘空间,大概有5,6个G

  7. stm32阅读代码工具source insight

    不知道学stm32有没有这样的烦恼,想看一个项目的代码,但是用keil又发现建立工程太麻烦,单个打开文件又找不到函数和变量之间的依赖关系,变量和函数又不能高亮显示,linux下vim和emacs虽然很 ...

  8. EasyUI Dialog 对话框 关闭事件

    在  $('#×××').dialog('close');  执行后触发 $(function(){ $("#titledialos").dialog({ onClose: fun ...

  9. zookeeper 单机版配置

    zookeeper :中间件,为分布式系统进行协调服务 作用于分布式系统,可以为大数据服务 支持java 和 C 客户端的api zookeeper 特性:一致性,数据会按照顺序分批入库: 原子性:数 ...

  10. linux命令学习之:chown

    chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷贝 ...