springboot 切面拦截自定义注解

使用切面来拦截被该注解标记的方法

  • 依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

1. 定义自定义注解

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 MyCustomAnnotation {
    // 可以根据需要定义注解的属性
    String value() default "";
}

上述代码定义了一个名为 MyCustomAnnotation 的自定义注解,@Retention(RetentionPolicy.RUNTIME) 表示该注解在运行时仍然有效,@Target(ElementType.METHOD) 表示该注解只能用于方法上。

2. 定义切面类

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class CustomAnnotationAspect {

    // 定义切入点表达式,拦截被 MyCustomAnnotation 注解标记的方法
    @Before("@annotation(myCustomAnnotation)")
    public void beforeMethod(JoinPoint joinPoint, MyCustomAnnotation myCustomAnnotation) {
        System.out.println("方法 " + joinPoint.getSignature().getName() + " 执行前,自定义注解属性值: " + myCustomAnnotation.value());
    }

    @After("@annotation(myCustomAnnotation)")
    public void afterMethod(JoinPoint joinPoint, MyCustomAnnotation myCustomAnnotation) {
        System.out.println("方法 " + joinPoint.getSignature().getName() + " 执行后,自定义注解属性值: " + myCustomAnnotation.value());
    }
}

在这个切面类中:
@Aspect 注解表明该类是一个切面类。
@Before(“@annotation(myCustomAnnotation)”) 定义了一个前置通知,当被 MyCustomAnnotation 注解标记的方法执行前,这个方法会被调用。
@After(“@annotation(myCustomAnnotation)”) 定义了一个后置通知,当被 MyCustomAnnotation 注解标记的方法执行后,这个方法会被调用。
JoinPoint 参数可以获取到被拦截方法的相关信息,如方法签名等。

3. 使用自定义注解

import org.springframework.stereotype.Service;

@Service
public class MyService {

    @MyCustomAnnotation(value = "示例注解属性值")
    public void myMethod() {
        System.out.println("执行 myMethod 方法");
    }
}

在 MyService 类的 myMethod 方法上使用了 MyCustomAnnotation 注解。

4. 启用

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@SpringBootApplication
@EnableAspectJAutoProxy
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

运行测试代码后,会看到在 myMethod 方法执行前后,切面类中的前置通知和后置通知方法会被调用,并输出相应的日志信息。
这就是使用切面拦截自定义注解的基本步骤和示例,你可以根据实际需求对注解和切面进行更复杂的扩展和定制。

### Anaconda 安装教程 #### 一、安装 Anaconda 并配置环境变量 为了确保 Anaconda 正常工作,在安装过程中需要注意勾选添加路径选项,这一步骤对于后续命令行工具的正常使用至关重要[^3]。 ```bash # 如果在安装时未选择添加路径,则可以通过手动设置环境变量来解决 export PATH="/path/to/anaconda/bin:$PATH" ``` #### 二、验证安装是否成功 完成安装后,可通过启动 Anaconda Navigator 或者在命令提示符中运行 `conda --version` 来确认安装情况。如果显示 Conda 的版本号则表示安装无误。 ```bash conda --version ``` ### 使用清华大学镜像加速软件包下载 #### 三、配置 Conda 使用清华大学镜像源 由于官方仓库可能速度较慢,建议修改默认的 Conda 源为清华大学开源软件镜像站提供的国内镜像地址,从而提高依赖项获取效率[^1]。 ```bash # 添加清华镜像作为优先级最高的渠道 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ # 显示完整的 URL 地址以便于调试 conda config --set show_channel_urls yes ``` #### 四、创建并激活 TensorFlow 所需虚拟环境 考虑到不同 Python 版本之间的兼容性差异,推荐专门为 TensorFlow 创建独立的工作空间,并指定合适的 Python 解释器版本[^2]。 ```bash # 基于特定Python版本建立新的Conda环境 conda create -n tensorflow python=3.6 anaconda # 切换至刚创建好的环境中去 conda activate tensorflow ``` #### 五、利用 Pip 和 Conda 结合方式高效部署 TensorFlow 及其他库 除了通过 Conda 管理基础设施外,还可以借助 PyPI 上丰富的第三方扩展资源进一步完善开发平台建设;同样地,这里也提倡采用本地化存储节点以加快网络传输速率[^4]。 ```bash # 运用Pip配合清华简易索引服务器快速加载目标模块 pip install tensorflow-gpu==2.x.y -i https://pypi.tuna.tsinghua.edu.cn/simple ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值