Spring的AOP中2个重要的注解
2023-09-11 14:16:49 时间
AOP中2个重要的注解
@Aspect
/**
* Aspect declaration
*
* @author Alexandre Vasseur
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Aspect {
/**
* @return the per clause expression, defaults to singleton aspect.
* Valid values are "" (singleton), "perthis(...)", etc
*/
public String value() default "";
}
说明:
作用:
声明当前类是一个切面类。
属性:
value:
默认我们的切面类应该为单例的。但是当切面类为一个多例类时,指定预
处理的切入点表达式。
用法是perthis(切入点表达式)。
它支持指定切入点表达式,或者是用@Pointcut修饰的方法名称(要求全限定方法名)
使用场景:
此注解也是一个注解驱动开发aop的必备注解。
示例:
Component
@Scope("prototype")//注意:通常情况下我们的切面类是不需要多例的。
@Aspect("perthis(execution(* com.dalianpai.service.impl.*.*(..)))")
public class LogUtil {
/**
* 用于配置当前方法是一个前置通知
*/
@Before("execution(* com.dalianpai.service.impl.*.*(..))")
public void printLog(){
System.out.println("执行打印日志的功能");
}
}
如果是多例模式,就要用perthis不然会报错,而且是当都有表达式的时候,切面上的表达式生效
@Pointcut
源码:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Pointcut {
/**
* The pointcut expression
* We allow "" as default for abstract pointcut
*/
String value() default "";
/**
* When compiling without debug info, or when interpreting pointcuts
at runtime,
* the names of any arguments used in the pointcut are not available.
* Under these circumstances only, it is necessary to provide the arg
names in
* the annotation ‐ these MUST duplicate the names used in the
annotated method.
* Format is a simple comma‐separated list.
*/
String argNames() default "";
}
说明:
作用:
此注解是用于指定切入点表达式的。
属性:
value:
用于指定切入点表达式。表达式的配置详解请参考第五章节第三小节《切
入点表达式的写法》
argNames:
用于指定切入点表达式的参数。参数可以是execution中的,也可以是args中的。通常情况下不使用此属性也可以获得切入点方法参数。
使用场景:
在实际开发中,当我们的多个通知需要执行,同时增强的规则确定的情况下,就可以把切入点表达式通用化。此注解就是代替xml中的<aop:pointcut>标签,实现切入点表达式的通用化。
实列:
@Component
@Aspect
public class LogUtil {
/**
* 通用切入点表达式
* 在value属性的中使用了&&符号,表示并且的关系。
* &&符号后面的args和execution一样,都是切入点表达式支持的关键字,表示匹配参数。指定的内容
* 可以是全限定类名,或者是名称。当指定参数名称时,要求与方法中形参名称相同。
* argNames属性,是定义参数的名称,该名称必须和args关键字中的名称一致。
*/
@Pointcut(value = "execution(* com.dalianpai.service.impl.*.*(com.dalianpai.domain.User))&& args(user)",argNames = "user")
private void pt1(User user){}
}
@Pointcut(value = "execution(* com.dalianpai.spring5.aop.service.impl.*.saveUser(..)) && args(user)")
private void pointcut1(User user){};
/**
* 用于配置当前方法是一个前置通知
*/
@Before(value = "pointcut1(user)",argNames = "user")
public void printLog(User user){
System.out.println("执行打印日志的功能"+user);
}
当然也可以把切入点的方法单独提取出来也没有问题,不一定要在切面中
相关文章
- Spring注解 AOP@Aspect的详细介绍
- 8 -- 深入使用Spring -- 4...4 Spring 的 AOP 支持
- 8 -- 深入使用Spring -- 0...
- 7 -- Spring的基本用法 -- 3... Spring 的核心机制 : 依赖注入
- Spring全家桶 源码 入门系列(二) --------AOP深度剖析
- Is Spring Integration 5.5.10 must be based on Java 9 to use or depend on spring integration? #3761
- 阿里 JAVA 架构师面试 136 题含答案:JVM+spring+ 分布式 + 并发编程!
- Spring AOP 扫盲
- spring batch(二):核心部分(1):配置Spring batch
- 比较分析 Spring AOP 和 AspectJ 之间的差别
- Spring Boot 2.X 国际化
- Spring Boot 2.x基础教程:使用JTA实现多数据源的事务管理
- 基于SSM(spring+springmvc+mybatis)+MySQL开发的新闻推荐系统【100010074】
- aop注解 spring提供的事务
- spring中的AOP 以及各种通知 配置
- Spring 基于注解的AOP实现
- Spring MVC 学习笔记 spring mvc Schema-based configuration
- spring框架漏洞整理(Spring Boot Actuator命令执行漏洞)
- Spring Aop——给Advice传递参数
- spring aop无法拦截类内部的方法调用
- Spring的Aop切面xml配置和注解(基于AspectJ)
- Spring AOP(AOP概念、组成、Spring AOP实现及实现原理)
- 全栈开发实战|人事管理系统的设计与实现(Spring Boot + Vue 3 + MyBatis)
- 【spring框架】XML配置AOP
- 【spring框架】AspectJ动态代理实现AOP
- spring cloud 搭建(JPA数据访问)
- 基于@AspectJ配置Spring AOP之一--转
- 曹工说Spring Boot源码(18)-- Spring AOP源码分析三部曲,终于快讲完了 (aop:config完整解析【下】)