环绕通知(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. openstack(Pike 版)集群部署(八)--- 连接Ceph Cluster 作为后端存储

    一.openstack Glance + ceph Cluster 部署: 博客:http://www.cnblogs.com/weijie0717/p/8563294.html 参考 续 部分.  ...

  2. Flask中的蓝图(BluePrint)、

    蓝图 1.初识Flask蓝图(BluePrint) 创建一个项目然后将目录结构做成: user.py中的内容 from flask import Blueprint, render_template ...

  3. POJ-3984.迷宫问题(BFS + 路径输出)

    昨天中午做的这道题,结果蛙了一整天,就因为一行代码困住了,今天算是见识到自己有多菜了.流泪.jpg 本题大意:给一个5 * 5的迷宫,1表示墙壁,0表示通路,从左上角走到右下角并输出路径. 本题思路: ...

  4. f5会话保持导致站点访问故障

    1.环境: vs Service Port为443和80,vip为10.160.100.66,pool为10.160.45.42/43的8080.80的vs重新到443. 2.问题: 单独访问源站都没 ...

  5. python3之subprocess常见方法使用

    一.常见subprocess方法 1.subprocess.getstatusoutput(cmd) 官方解释: Return (exitcode, output) of executing cmd ...

  6. win8 IIS配置

    iis发展到win8已经到是iis8.0了.至于如何安装的,咱们接下来看.大体和iis6安装差别不大.iis8安装起来也是很方便的. 1.打开“控制面板”——“程序和功能”.如下图所示 2.点击“启用 ...

  7. awk参数解析

    # awk --help Usage: awk [POSIX or GNU style options] -f progfile [--] file ... Usage: awk [POSIX or ...

  8. Beanstalkd消息队列的安装与使用

    一.Beanstalkd是什么? Beanstalkd是一个高性能,轻量级的分布式内存队列 二.Beanstalkd特性 1.支持优先级(支持任务插队)2.延迟(实现定时任务)3.持久化(定时把内存中 ...

  9. JFinal文件上传时直接使用getPara()去接受表单的数据接收到的数据一直是null?

    解决方案: 在文件上传页面form 标签中使用: enctype="multipart/form-data" 在controller类中先调用getFile系列方法才能使getPa ...

  10. Liunx rm

    rm是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比如在/(根目录)下执行rm * -rf,这是一个危险的动作,“ * ”代表任意个字符).所以,我们在执行rm之 ...