第一步:编写通知类

package com.airplan.pojo;

import org.aspectj.lang.ProceedingJoinPoint;

public class Advice {

        public void mybefore(String name1,int age1){
System.out.println("前置"+name1 );
} public void mybefore1(String name1){
System.out.println("前置:"+name1);
} public void myaftering(){
System.out.println("后置 2");
} public void myafter(){
System.out.println("后置 1");
} public void mythrow(){
System.out.println("异常");
} public Object myarround(ProceedingJoinPoint p) throws Throwable{ System.out.println("执行环绕");
System.out.println("环绕-前置");
Object result = p.proceed();
System.out.println("环绕后置");
return result;
}
}

第二步配置

2 配置 spring 配置文件

  2.1 <aop:after/> 后置通知,是否出现异常都执行

  2.2 <aop:after-returing/> 后置通知,只有当切点正确执行时执行

  2.3 <aop:after/> 和 <aop:after-returing/> 和<aop:after-throwing/>执行顺序和配置顺序有关

  2.4 execution() 括号不能扩上 args
  2.5 中间使用 and 不能使用&& 由 spring 把 and 解析成&&
  2.6 args(名称) 名称自定义的.顺序和 demo1(参数,参数)对应
  2.7 <aop:before/> arg-names=” 名 称 ” 名 称 来 源 于expression=”” 中 args(),名称必须一样

    2.7.1 args() 有几个参数,arg-names 里面必须有几个参数
    2.7.2 arg-names=”” 里面名称必须和通知方法参数名对应

<aop:config>
<aop:aspect ref="myadvice">
<!--
args(name1,age1):args中的参数必须与<aop:before />标签中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"/> </aop:aspect>
</aop:config>

上面标蓝的部分一定要一致,并且要与通知方法的参数对应,连名称也要一样,虽然切点的参数名称不要求和args中的名称对应,但是数量要对应

spring学习 十二 AspectJ-based的通知入门 带参数的通知的更多相关文章

  1. Spring学习(十二)-----Spring @PostConstruct和@PreDestroy实例

    实现 初始化方法和销毁方法3种方式: 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高) 设置bean属性 Init-method destroy- ...

  2. Spring学习(十二)-----Spring Bean init-method 和 destroy-method实例

    实现 初始化方法和销毁方法3种方式: 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高) 设置bean属性 Init-method destroy- ...

  3. Spring学习十二----------Bean的配置之@ImportResource和@Value

    © 版权声明:本文为博主原创文章,转载请注明出处 @ImportResource -引入XML配置文件 @Value -从配置文件中获取值 实例 1.项目结构 2.pom.xml <projec ...

  4. spring学习十二 application/x-www-form-urlencoded还是application/json

    application/x-www-form-urlencoded还是application/json get. POST 用哪种格式? 后台如何得到这些值? 如何用ajax  或者是 postman ...

  5. Spring Boot(十二):spring boot如何测试打包部署

    Spring Boot(十二):spring boot如何测试打包部署 一.开发阶段 1,单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. (1)在p ...

  6. spring 学习(二):spring bean 管理--配置文件和注解混合使用

    spring 学习(二)spring bean 管理--配置文件和注解混合使用 相似的,创建 maven 工程,配置pom.xml 文件,具体可以参考上一篇博文: sprint 学习(一) 然后我们在 ...

  7. (转)SpringMVC学习(十二)——SpringMVC中的拦截器

    http://blog.csdn.net/yerenyuan_pku/article/details/72567761 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter, ...

  8. spring学习 十一 AspectJ-based的通知入门 不带参数的通知

    AspectJ-Based方式的AOP,通知类不需要实现任何接口,且前置通知,后置通知,环绕通知,异常通知都可以写在一个类中,下面先实现一个简单的,不带参数的通知. 第一步定义通知类,通知类中的通知( ...

  9. iOS NSNotification传递带参数的通知

    普通的通知使用 注册观察者 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotificat ...

随机推荐

  1. oracle 中更新update不成功的原因

    oracle 中执行insert into 与delete 都正常,但是执行update 却没有反应. 原因: 是因为记录锁.这种只有update无法执行其他语句可以执行的其实是因为记录锁导致的,在o ...

  2. python写机器人玩僵尸骰子

    python写机器人玩僵尸骰子由Al Sweigart用python发布注意:我正在为我的僵尸骰子模拟器寻找反馈,以及这一套指令.如果你觉得有什么地方可以改进,请发邮件到al@inventwithpy ...

  3. [转]github详细教程

    GitHub详解 GitHub 是一个共享虚拟主机服务,用于存放使用Git版本控制的软件代码和内容项目.它由GitHub公司(曾称Logical Awesome)的开发者Chris Wanstrath ...

  4. Disruptor并发框架简介

    Martin Fowler在自己网站上写一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金额交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JVM平台上,其核心是一个业务逻辑处 ...

  5. c#编写windows服务在开机是OnStart启动超时

    1.编写服务对应的config文件, 比如我的服务叫ModbusAgent.exe,对应的文件叫ModbusAgent.exe.config 文件内容: <?xml version=" ...

  6. Redhat ssh服务登录慢

    redhat在安装以后每次通过ssh服务登录,要等待几秒才能进入. 只要在sshd_config修改一下以下值就好 vim /etc/ssh/sshd_config UseDNS no service ...

  7. TF30063:没有访问xxx的权限 vs2017

    凭据什么的都删掉了,还是无法连接上,最后同事用一种方法o(╥﹏╥)o,先登录vs2015的tfs账号,然后再vs2017上面登录tfs账号即可解决.PS:网上很多文章都是抄的,这个Bug真TM恶心.

  8. C# Request.RawUrl与Request.Url的区别

    RawUrl——不包含域名及端口的地址 Url——包含域名,最全

  9. webstorm配置node.js

    1.确保电脑已经安装好了node.js

  10. HTML标签归纳

    首先,按下h1,再按下Tab键就可以变成<h1></h1>,Ctrl+d可以复制当前行,Ctrl+Alt+Insert可以快速新建文件,Home可以跳到当前行最前面,End同理 ...