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 ...
随机推荐
- vue项目中,localhost可以访问,IP无法访问的问题
用http://localhost:8082/可以访问,但是换到ip就访问不了,127.0.0.1.0.0.0.0访问也可以,就ip不行 根源----在config里面的index.js里面的modu ...
- python网络爬虫《http和https协议》
一.HTTP协议 1.官方概念: HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文 ...
- Python+Selenium学习--分页处理
场景 我们在测试一个web 应用时,经常出现翻页的情况,下面介绍翻页场景 代码 #!/usr/bin/env python # -*- codinfg:utf-8 -*- ''' @author: J ...
- 常用的key和oid
1.FortiGate Template-Network-Office-Fortigate-Session Count:key,fgSysSesCount oid,.1.3.6.1.4.1.123 ...
- redis(三)积累-基本的取值和设值
1. 先把redis的连接池拿出来, JedisPool pool=new JedisPool(new JedisPoolConfig(),"127.0.0.1") Jedis ...
- cloud server ribbon 自定义策略配置
虽然ribbon默认为我们提供了多钟负载均衡策略,但有时候我们仍然需要自定义符合自身业务逻辑的规则 使用配置文件的方式:我们只需要在配置文件中添加配置 serviceId.ribbon.NFLoadB ...
- 设置TextFiled输入长度限制
#pragma mark - 显示超过11位不让输入 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange: ...
- DMZ原理与应用
DMZ是英文“demilitarized zone”的缩写,中文名称为“隔离区”,“非军事化区”.它是为了解决安装防火墙后外部网络不能访问内部网络服务器的问题,而设立的一个非安全系统与安全系统之间的缓 ...
- SQL update语句 更新和查询同一张表 冲突
#update 和 select在同一张表的时候会显示冲突 报错信息: [Err] 1093 - You can't specify target table 'tb_a' for update i ...
- make ;makefile; cmake; qmake的区分
1. make 是用来执行Makefile的.2. Makefile是类unix环境下(比如Linux)的类似于批处理的"脚本"文件.其基本语法是: 目标+依赖+命令,只有在目标文 ...