spring学习 十四 注解AOP 通知传递参数
我们在对切点进行增强时,不建议对切点进行任何修改,因此不加以使用@PointCut注解打在切点上,尽量只在Advice上打注解(Before,After等),如果要在通知中接受切点的参数,可以使用JoinPoint或者ProceedingJoinPoint
在Spring AOP中可以通过两种方式传递参数给Advice(通知)
(1)通过接受JoinPoint(非环绕通知)或ProceedingJoinPoint(环绕通知)参数,其实ProceedingJoinPoint是JointPoint的子接口
(2) 还可以直接 接收与切入点方法执行有关的对象,比如切入点方法参数、切入点目标对象(target)、切入点代理对象(this)等。
第一步创建切面类
package com.airplan.pojo; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; @Component("advice")
@Aspect
public class Advice { @After("execution(* com.airplan.pojo.PointCutClass.*(..))")
public void myafter(JoinPoint jp){
System.out.println("前置通知");
} }
第二步创建切点类,并没有在任何切点上打上@PointCut注解,也就是核心关注点并不会感知到增强
package com.airplan.pojo; import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Component("pointCutClass")
public class PointCutClass { public void pointCutFun() { System.out.println("切点执行");
} public void pointCutFun02(String name) {
System.out.println("切点执行");
} }
第三步;测试代码
package com.airplan.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.airplan.pojo.Advice;
import com.airplan.pojo.PointCutClass; public class AopDemo {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
PointCutClass pc = ac.getBean("pointCutClass",PointCutClass.class);
pc.pointCutFun02("张三");
//pc.pointCutFun(); }
}
JoinPoint解说:
访问目标方法最简单的做法是定义增强处理方法时,将第一个参数定义为JoinPoint类型,当该增强处理方法被调用时,该JoinPoint参数就代表了织入增强处理的连接点。JoinPoint里包含了如下几个常用的方法:
Object[] getArgs:返回目标方法的参数
Signature getSignature:返回目标方法的签名
Object getTarget:返回被织入增强处理的目标对象
Object getThis:返回AOP框架为目标对象生成的代理对象
spring学习 十四 注解AOP 通知传递参数的更多相关文章
- spring学习 十二 AspectJ-based的通知入门 带参数的通知
第一步:编写通知类 package com.airplan.pojo; import org.aspectj.lang.ProceedingJoinPoint; public class Advice ...
- Spring MVC(十)--通过表单序列化传递参数
通过表单序列化传递参数就是将表单数据转化成字符串传递到后台,序列化之后参数请求变成这种模式param1=value1&¶m2=value2,下面用代码实现. 1.创建表单 &l ...
- Spring学习十四----------Spring AOP实例
© 版权声明:本文为博主原创文章,转载请注明出处 实例 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...
- Spring 学习(四)--- AOP
问题 : AOP 解决的问题是什么 Spring AOP 的底层实现是什么 Spring AOP 和 AspectJ 的区别是什么 概述 在软件业,AOP为Aspect Oriented Progra ...
- spring学习笔记四:AOP
AOP(Aspect Orient Programming),面向切面编程,是对面向对象编程OOP的一种补充 面向对象编程使用静态角度考虑程序的结构,而面向切面编程是从动态角度考虑程序运行过程 AOP ...
- Spring 学习十四 Spring security安全
Spring security: 我用过的安全机制: oauth2, filter, secured方法保护 9.2 保护web请求: 9.2.1 代理Servlet过滤器: Delegat ...
- Spring学习(十四)----- Spring Auto Scanning Components —— 自动扫描组件
一. Spring Auto Scanning Components —— 自动扫描组件 1. Declares Components Manually——手动配置componen ...
- Spring的第四天AOP之注解版
Spring的第四天AOP之注解版 ssm框架 spring 在上一篇博客中,介绍了Spring的AOP的xml版本的使用,在这篇博客中,我将介绍一下,注解版的使用. 常用注解 注解 通知 @Aft ...
- SpringBoot进阶教程(六十四)注解大全
在Spring1.x时代,还没出现注解,需要大量xml配置文件并在内部编写大量bean标签.Java5推出新特性annotation,为spring的更新奠定了基础.从Spring 2.X开始spri ...
随机推荐
- ss源码学习--从协议建立到完成一次代理请求
上一次介绍了ss源码中各个事件处理函数完成的工作,这次具体分析一下协议的建立以及请求数据的传输过程. 因为ss的local和server共用一个类以及一系列的事件处理函数,所以看起来稍显复杂.下面来将 ...
- 齐博CMS:最常用的一些变量名,方便二次开发.必须要熟悉的
ROOT_PATH程序所在硬盘目录的绝对地址,等同于以前的PHP168_PATH$webdb网站的全局变量,模块的全局变量也是这个$onlineip当前用户的IP$timestamp当前时间$WEBU ...
- this.class.getClassLoader().getResourceAsStream与this.class.getResourceAsStream 文件地址获取
本文部分转自:http://xixinfei.iteye.com/blog/1256291 this.getClass().getClassLoader().getResource("tem ...
- 测试用户体验相关——UI设计准则及方法
之前跟我们uxc同学聊过一些,记录下来,方便在工作中不断渗透深入和理解,能够逐渐养成比较好的审美和对UI交互问题的敏锐的觉察力. 以问题为导向来吧... 第一个问题:一个menu中的图标一定要风格一致 ...
- f5 Seldom used
1.负载均衡算法 2)最快响应速度(Fastest) •优先查看7层请求的连接数,然后查看4层连接数 •需要在virtual server上关联7层的profile,否则与最小连接数相同 •后台服务器 ...
- swift 数组 的一些快速方法
1. filter (过滤器):返回符合条件的一个数组 let arr = [1,5,6,7,10,0] //写法1 let arr1 = arr.filter { (item) -> Bool ...
- SCP对拷如何连接指定端口远程主机
SCP对拷如何连接指定端口(非22端口)的远程主机 scp -P 远程端口号 本机待拷贝文件路径 远程主机用户名@远程主机地址:远程主机目的路径 注意: 1. 参数-P 的位置一定要紧跟在s ...
- Bootstrap(2) 排版样式
1.页面主体,Bootstrap 将全局 font-size 设置为 14px,line-height 行高设置为 1.428(即20px):<p>段落元素被设置等于 1/2 行高(即 1 ...
- 瑞波Ripple-OnlineDeletion配置的理解
1.概要 如果没有设置online deletion,那么Rippled Server的本地数据库大小将会无限增长.为了释放磁盘空间,那种情况应该被制止并且那些数据库文件应该被手动删除.online ...
- 更改AVD默认路径
默认情况下,安卓模拟器镜像文件会放到%userprofile%\.android下,例如当前Win7登录用户为administrator 则%userprofile%为 c:\users\admini ...