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 ...
随机推荐
- 【pyspider】启动爬虫后在results页面没有看到结果
今天根据书上的介绍写了一个简单爬虫,爬取豌豆荚里面APP的基本信息,但是在调试结果正常后,发现跳转到result页面后没有看到结果. 后来上网查了一下,发现要在def detail_page(self ...
- NumPy IO
NumPy IO Numpy 可以读写磁盘上的文本数据或二进制数据. NumPy 为 ndarray 对象引入了一个简单的文件格式:npy. npy 文件用于存储重建 ndarray 所需的数据.图形 ...
- Hadoop特点
一:HDFS 1.HDFS上传数据,会将文件切分成指定大小的数据块,并以多副本的数据块存储在机器上. 2. part0是指 副本有2个而且1,2有两个副本 二.YARN 1.负责整个集群的管理和调度 ...
- 190. Reverse Bits (Int; Bit)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 装了appserv之后,浏览器中访问localhost加载不了
AppServe下载地址:https://AppServnetwork.com/ 如果只下载Apache,推荐大神博客http://www.cnblogs.com/zhaoqingqing/p/496 ...
- java序列化的认识(从多本书和多个博客中的总结)
Serializable接口是java.io下的一个标记接口,一个类要被序列化必须实现这个接口.
- angular实现链接锚记
前言: 之所以这么说,是因为angular的路由将html默认的链接锚记的#给占用了,所以传统的链接锚记在这里将不再适用,这个有点坑啊,又要多写好几行代码来模拟这个功能. 实现原理: location ...
- MySQL基本SQL语句之数据插入、删除数据和更新数据
一.INSERT插入数据: 方法一:批量插入 基本语法: INSERT INTO tb_name (col1, col2, ...) VALUES (val1, val2, ...)[,(val1, ...
- cloud配置中心遇到的坑
https://blog.csdn.net/z960339491/article/details/80593982分布式配置中心为什么要有用分布式配置中心这玩意儿?现在这微服务大军已经覆盖了各种大小型 ...
- iOS - 抖音效果
抖音的转场动画—iOS https://www.jianshu.com/p/29b0165de712 抖音的上下滑实现—iOS https://www.jianshu.com/p/e8799510c7 ...