第一步:编写通知类

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. 【OpenGL】第一个窗口

    包含头文件: #include <GL/glew.h> // GLFW #include <GLFW/glfw3.h> 初始化与配置GLFW: glfwInit(); //初始 ...

  2. jdk1.8的项目在jdk1.7的环境下运行

  3. Django常用的模板标签

    django 目前了解的各个文件的作用: manage.py:  运行服务 urls: 路由 views: 处理数据 传递给 html模板 html文件:  通过 {{变量名}}接收变量 通过 模板标 ...

  4. 包含min函数的栈(python)

    题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). # -*- coding:utf-8 -*- class Solution: #入栈时 ...

  5. anaconda的安装tensorflow

    在anaconda prompt中我们输入 anaconda search -t conda tensorflow 查看能在哪里安装tensorflow anaconda show dhirschfe ...

  6. Balanced Numbers (数位DP)

    Balanced Numbers https://vjudge.net/contest/287810#problem/K Balanced numbers have been used by math ...

  7. HDU 2680 Choose the best route(SPFA)

    Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...

  8. [剑指Offer]12-矩阵中的路径(回溯)

    题目链接 https://www.nowcoder.com/practice/c61c6999eecb4b8f88a98f66b273a3cc?tpId=13&tqId=11218&t ...

  9. 2017-2018-2 20165315 实验二《Java面向对象程序设计》实验报告

    2017-2018-2 20165315 实验二<Java面向对象程序设计>实验报告 一.实验内容及步骤 1.初步掌握单元测试和TDD 单元测试 任务一:三种代码 用程序解决问题时,要学会 ...

  10. linux命令学习之:passwd

    passwd命令用于设置用户的认证信息,包括用户密码.密码过期时间等.系统管理者则能用它管理系统用户的密码.只有管理者可以指定用户名称,一般用户只能变更自己的密码. 语法 passwd(选项)(参数) ...