首页 > 其他分享 >SpringAOP增强-几种不同将注解和切面方法的关联方式

SpringAOP增强-几种不同将注解和切面方法的关联方式

时间:2024-03-31 13:45:49浏览次数:12  
标签:around void annotation 切面 SpringAOP 注解 方法 public

@Around的作用

  • 既可以在目标方法之前织入增强动作,也可以在执行目标方法之后织入增强动作;
  • 可以决定目标方法在什么时候执行,如何执行,甚至可以完全阻止目标目标方法的执行;
  • 可以改变执行目标方法的参数值,也可以改变执行目标方法之后的返回值; 当需要改变目标方法的返回值时,只能使用Around方法;
  • 虽然Around功能强大,但通常需要在线程安全的环境下使用。因此,如果使用普通的Before、AfterReturing增强方法就可以解决的事情,就没有必要使用Around增强处理了。

注解方式:如果需要对某一方法进行增强,只需要在相应的方法上添加上自定义注解即可

 

注解类:

package com.rq.aop.common.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)//运行时有效
@Target(ElementType.METHOD)//作用于方法
public @interface MyAnnotation {
    String methodName () default "";
}

 

将注解和切面方法的关联方式 ---第一种切点方式:

@Around(value = "@annotation(around)") //around 与 下面参数名around对应
    public void processAuthority(ProceedingJoinPoint point,MyAnnotation around) throws Throwable{
.....
}
注解上加
@annotation(变量名称),
下面参数加 注解类型+变量名称,
注意,这两个变量名称必须是一样的,这样在加@MyAnnotation 注解方法上,就能切入到此方法上了。
package com.rq.aop.common.advice;

import com.rq.aop.common.annotation.MyAnnotation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect //标注增强处理类(切面类)
@Component //交由Spring容器管理
public class AnnotationAspect {

    /*
    可自定义切点位置,针对不同切点,方法上的@Around()可以这样写ex:@Around(value = "methodPointcut() && args(..)")
    @Pointcut(value = "@annotation(com.rq.aop.common.annotation.MyAnnotation)")    
    public void methodPointcut(){}

    @Pointcut(value = "@annotation(com.rq.aop.common.annotation.MyAnnotation2)")
    public void methodPointcut2(){}
    */

    //定义增强,pointcut连接点使用@annotation(xxx)进行定义
    @Around(value = "@annotation(around)") //around 与 下面参数名around对应
    public void processAuthority(ProceedingJoinPoint point,MyAnnotation around) throws Throwable{
        System.out.println("ANNOTATION welcome");
        System.out.println("ANNOTATION 调用方法:"+ around.methodName());
        System.out.println("ANNOTATION 调用类:" + point.getSignature().getDeclaringTypeName());
        System.out.println("ANNOTATION 调用类名" + point.getSignature().getDeclaringType().getSimpleName());
        point.proceed(); //调用目标方法
        System.out.println("ANNOTATION login success");
    }
}

 

将注解和切面方法的关联方式 

方式二:直接在切面方法上 指定注解路径

@Before(value = "@annotation(com.cms.common.annotation.ControllerOptLog)")  //这里直接 指定注解的位置即可(注解的全路径)。 然后再需要添加注解的方法A上加这个注解,那方法A就能被当前的beforeAdvice()方法所切入。
public void beforeAdvice(JoinPoint joinPoint) throws Throwable { }




@ControllerOptLog public void 方法A(参数类型,变量) throws Exception

{ .....

..... ....... }

 

方式三:

先将这个注解 和某个方法绑定,然后再在@before注解中指定这个方法: 其实可以理解成是一个间接关联。

注解关联到-方法    方法再关联到曾强的具体逻辑处。

    /**
     * 定义作为切入点的方法 ,并且将切入方法和@ParamValided 关联起来
     */
    @Pointcut("@annotation(com.atguigu.gulimall.coupon.learn.annotation.ParamValided)")  //这里是注解的定义全路径
    public void pointcut(){};

    @Before(value="pointcut()")
    public void before(JoinPoint joinPoint) throws Exception{
            .................
    }

@ParamValided

public void 方法A(参数类型,变量) throws Exception

{ ..........

....... }



 

标签:around,void,annotation,切面,SpringAOP,注解,方法,public
From: https://www.cnblogs.com/isme-zjh/p/18106648

相关文章

  • Spring AOP 和 拦截器 获取类上与方法上的注解
    在做一个获取目标注解的鉴权功能时,想到了AOP与拦截器两种方式,其中@HasPermission是我自定义的注解,以下分别为AOP与拦截器获取访问目标类与方法上的注解的方法。由于我的系统在拦截器上配置了拦截过则,所以我选的是拦截器的方式,读者可根据自己的需求来。一、SpringAOP方式获取......
  • 深入浅出Spring AOP:面向切面编程的实战与解析
    导语SpringAOP(面向切面编程)作为Spring框架的核心特性之一,提供了强大的横切关注点处理能力,使得开发者能够更好地解耦系统架构,将非功能性需求(如日志记录、事务管理、权限控制等)从主业务逻辑中抽离出来,实现模块化的交叉关注点处理。本文将带你逐步探索SpringAOP的关键技术要点......
  • @Transactional详解(作用、失效场景与解决方法)| 事务注解实际原理(AOP)解析
    开发中代码实现事务的方式,理论上说有两种:编程式事务、注解式事务。但是实际上使用最多的还是注解实现的事务控制; 1、编程式事务(开发用的很少了)基于底层的API,如PlatformTransactionManager、TransactionDefinition 和 TransactionTemplate 等核心接口,开发者完全可以通过编......
  • java使用注解实现系统日志记录
    不论在神魔类型的项目中,日志系统绝对是一个不可少的存在,那么,怎末用一个最简便的方式来实现日志在数据库中的存储呢??最近在项目中正好负责了日志模块的实现,就简单记录一下。我在这个项目中使用的是aop自定义注解的方式,大致步骤如下:1.第一步,首先需要先定义一个注解类,来实现部分方法......
  • 注解处理器
    demo:/1:***定义注解*/@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic@interfaceFruitProvider{/**供应商编号*/publicintid()default-1;/***供应商名称*/publicStringname()default"";13/04/2018Page108of283......
  • @PathVariable注解
      @PathVariable 是SpringMVC中的一个注解,用于将URL中的模板变量绑定到方法的参数上。在SpringMVC中,通常会使用RESTful风格的URL来处理请求,URL中可能包含一些占位符,例如/users/{id},其中{id}就是一个占位符,用来表示一个参数,如进行逻辑删时,在请求头直接添加......
  • 【项目实战】记一次因单元测试注解@BeforeEach 和@Before错误使用导致的空指针异常
    一、错误说明在项目开发过程中,我们可能会遇到因单元测试注解@BeforeEach和@Before错误使用导致的空指针异常。在使用JUnit5框架时,错误地使用了@Before注解,导致在每个测试方法执行前没有正确初始化对象,从而引发空指针异常。二、报错内容为了解决这个问题,需要了解这两个注......
  • 基于注解方式缓存本地数据
    一、简介之前一直在忙,没有时间写一篇基于注解的缓存本地数据,因为有的小的业务没必要引入redis中间件,所以我个人觉得还是得根据自己的业务场景去使用。二、实战代码1.引入依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-b......
  • SpringBoot 常用注解总结
    核心注解1.@SpringBootApplication主要用于开启自动配置,它也是一个组合注解,主要组合了@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan2.@EnableAutoConfiguration该注解组合了@Import注解,@Import注解导入了EnableAutoCofigurationImportSelector......
  • 面向对象【Annotation注解】
    文章目录注解概述注解与注释常见的Annotation最基本的注解使用@Override@Override@SuppressWarnings元注解@Retention@Target@Documented@Inherited自定义注解格式定义使用注解概述注解(Annotation)是从JDK5.0开始引入,以“@注解......