首页 > 其他分享 >SpringBoot中两种常用定位切点的方法

SpringBoot中两种常用定位切点的方法

时间:2024-01-31 15:57:18浏览次数:16  
标签:定位 SpringBoot 自定义 切点 切面 execution 方法 public proceed

有时候,我们使用AOP来进行放的增强,编写切面类的时候,需要定位在哪个方法上试用该切面进行增强,本文主要讲解两种在SpringBoot中定位切点的方法,一种是使用execution表达式的方法,一种则是利用自定义注解的方法。
execution 表达式
execution表达式的方式主要是在定义切点的时候,通过表达式的方式选取到所需要增强的方法。

execution表达式解读
接下来以一个简单的例子来讲解这两种方法的使用方式。

<==========方法执行前==========>
method();
<==========方法执行后==========>

execution 表达式
execution表达式的方式主要是在定义切点的时候,通过表达式的方式选取到所需要增强的方法。

execution表达式解读

execution(<修饰符模式>?<返回类型模式><方法名模式>(<参数模式>)<异常模式>?)

// 匹配指定包中的所有方法
execution(* com.luke.service.*(..))
 
// 匹配当前包中的所有public方法
execution(public * UserService.*(..))
 
// 匹配指定包中的所有public方法,并且返回值是int类型的方法
execution(public int com.luke.service.*(..))
 
// 匹配指定包中的所有public方法,并且第一个参数是String,返回值是int类型的方法
execution(public int com.luke.service.*(String name, ..))

自定义切面类:

@Aspect
@Component
public class LogAspect {
 
    @Pointcut("execution(* com.luke.springdata.controller.*.*(..))")
    public void operationLog(){}
 
    /**
     * 这里只定义一个Around的增强做展示
     */
    @Around("operationLog()")
    public Object doAround(ProceedingJoinPoint joinPoint) {
        Object proceed = null;
        try {
            System.out.println("方法执行前");
            proceed = joinPoint.proceed();
            System.out.println("方法执行后");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;
    }
}

自定义切面类:

@Aspect
@Component
public class LogAspect {
 
    @Pointcut("execution(* com.luke.springdata.controller.*.*(..))")
    public void operationLog(){}
 
    /**
     * 这里只定义一个Around的增强做展示
     */
    @Around("operationLog()")
    public Object doAround(ProceedingJoinPoint joinPoint) {
        Object proceed = null;
        try {
            System.out.println("方法执行前");
            proceed = joinPoint.proceed();
            System.out.println("方法执行后");
        } catch (Throwable throwable) {
            throwable.printStackTrace();<details>

        }
        return proceed;
    }
}

自定义切面类:

@Aspect
@Component
public class LogAspect {
 
    @Pointcut("execution(* com.luke.springdata.controller.*.*(..))")
    public void operationLog(){}
 
    /**
     * 这里只定义一个Around的增强做展示
     */
    @Around("operationLog()")
    public Object doAround(ProceedingJoinPoint joinPoint) {
        Object proceed = null;
        try {
            System.out.println("方法执行前");
            proceed = joinPoint.proceed();
            System.out.println("方法执行后");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;
    }
}

此切点的execution表达式为
com.luke.springdata.controller包下的所有方法。

使用@Around注解表明增强的方法,并且指定切点。

测试用Controller类

@RestController
@RequestMapping("/person")
public class PersonController {
 
    @GetMapping("/test")
    public void test(){
        System.out.println("方法执行了");
    }
    
}

运行项目,调用该方法,查看结果。

方法执行前
方法执行了
方法执行后

自定义注解的方法
自定义注解的方式就是在需要增强的方法上面加上自定义的注解即可。

自定义注解类:

@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Log{
    
}

这里自定义了一个注解Log,该注解只能加在方法上。

自定义切面类:

@Aspect
@Component
public class LogAspect {
 
    @Pointcut("@annotation(com.luke.springdata.annotation.Log)")
    public void operationLog(){}
 
    /**
     * 这里只定义一个Around的增强做展示
     */
    @Around("operationLog()")
    public Object doAround(ProceedingJoinPoint joinPoint) {
        Object proceed = null;
        try {
            System.out.println("方法执行前");
            proceed = joinPoint.proceed();
            System.out.println("方法执行后");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;
    }
}

这里编写的自定义个切面类,用@Pointcut注解定义一个切面,并且这次采用@annotation(xxx)的方式表明如果哪个方法上添加了xxx注解,则就使用该切面做增强。

同时在每个增强的方法上使用该切面,随后编写正常的方法增强逻辑即可。

测试用Controller类

@RestController
@RequestMapping("/person")
public class PersonController {
 
    @Log
    @GetMapping("/test")
    public void test(){
        System.out.println("方法执行了");
    }
    
}

此时在需要使用切面的方法上加入@Log注解,调用该方法,查看效果

方法执行前
方法执行了
方法执行后

总结
两种方式均能实现AOP的功能,在使用上,如果某个包下面的所有方法,都需要这个切面进行增强,那么使用execution表达式的方式更方便。但如果只有部分方法需要,并且分布在不同的类中,则注解的方式更灵活。

标签:定位,SpringBoot,自定义,切点,切面,execution,方法,public,proceed
From: https://www.cnblogs.com/codeLearn/p/17999388

相关文章

  • SpringBoot介绍
    SpringBoot一、介绍我们在搭建传统SSM项目时,通常需要导入很多依赖,但每种依赖又有许多版本,这就很容易出现依赖冲突。此外,繁琐的配置也是一大痛点,太多了,让人很难记住,因此SpringBoot诞生了。SpringBoot是对Spring的深度封装,基于约定优于配置的思想,提供了大量的默认配置和实现。......
  • 基于springboot开发的工作流系统,bpmn.js,vue源码及功能分析(activiti)
    前言activiti工作流引擎项目,企业erp、oa、hr、crm等企事业办公系统轻松落地,一套完整并且实际运用在多套项目中的案例,满足日常业务流程审批需求。一、项目形式springboot+vue+activiti集成了activiti在线编辑器,流行的前后端分离部署开发模式,快速开发平台,可插拔工作流服务。工作......
  • SpringBoot 整合多数据源的事务问题
    代码先贴代码:核心就是:Spring给我们提供的一个类AbstractRoutingDataSource,然后我们再写一个切面来切换数据源,肯定要有一个地方存储key还要保证上下文都可用,所以我们使用ThreadLocal来存储数据源的keypom.xml<dependency><groupId>org.springframework.boot</gr......
  • Java21 + SpringBoot3整合springdoc-openapi,自动生成在线接口文档,支持SpringSecurity
    目录前言相关技术简介OpenAPISwaggerSpringfoxspringdocswagger2与swagger3常用注解对比实现步骤引入maven依赖修改配置文件设置api-docs和swagger-ui访问权限定义springdoc配置类修改Controller类和实体类查看效果总结前言近日心血来潮想做一个开源项目,目标是做一款可以适配多......
  • springboot~logback控制日志文件大小与历史个数
    日志本地化,对于每个应用来说都是必须的,最起码第一时间日志是需要本地化的,然后从本地化的日志中再进行推送,例如通过fluentd将日志推到es里,通过kibana工具进行可视化分析。日志文件大小限制对于把日志持久化到磁盘,你需要设置它的占用空间,logback提供了保存文件大小和历史文件数量......
  • SpringBoot、SpringCloud相关技术
    一、三层架构 1.1统一业务处理1.2解决三层架构中的结偶,使用了Ioc(控制反转)和DI(依赖注入) 1.3切面编程、事务处理、全局异常处理、Cookie、Session、JWT令牌、阿里云OSS、Mybatis处理数据库二、技术分类说明  2.1SSM三、Web总结四、相关第三方插件https://www......
  • SpringBoot自定义注解+反射实现 excel 导入的数据组装及字段校验
    本次给大家带来的SpringBoot中通过自定义注解+反射实现excel导入数据组装及字段校验的实现方式。这种实现方式其实是很普通、常规的方法,但很多同学在开发过程中,可能却不太容易想到他。当然我也是众多同学中的一员。1背景在前段时间的开发工作中,接手了一个很简单,很普通的开发任务。......
  • Mybatis-plus分页查询(SpringBoot)
    2024-01-30OS:Windows10 22H2IDE:IDEA2022.2.5JDKversion:19Mavenversion:3.6.3数据库:MySQL8.1.0mybatis-plus:3.5.3.1 一、在springBoot启动类中将分页插件加入到ioc容器里面启动类 @SpringBootApplication@MapperScan("com.ssm.mapper")publicclassMain{......
  • springboot集成mqtt
    SpringBoot集成MQTT(简单版)一、docker安装emqx环境(Linux系统)emqx:mqtt服务器(broker)version:'3'services:emqx:image:emqx/emqxcontainer_name:emqxrestart:alwaysports:-8001:18083-8002:1883-8003:8083-8004......
  • Android原生定位
    使用LocationManager和LocationListener结合进行简单定位功能1.创建LocationManagerLocationManagerlocationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);2.创建LocationListenerprivatefinalLocationListenerlocationListener=newLocati......