Java通过自定义注解实现拦截类及其内部所有方法

Java通过自定义注解实现拦截类及其内部所有方法

SpringBoot项目自定义注解实现拦截类及其内部方法

1.引入AOP依赖

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

2.编写注解

import java.lang.annotation.*;

/**
 * @ClassName: ApiAuth
 * @Description: 自定义注解
 * @Author: luzihao
 * @Email: 458243101@qq.com
 * @Date: 2020/9/22 16:33
 */
// 适用于类、方法
@Target({ElementType.TYPE, ElementType.METHOD})
// 注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiAuth {
}

@Target:说明注解所修饰的范围

  • ElementType.TYPE:类
  • ElementType.METHOD:方法

@Retention:说明注解的生命力

  • RetentionPolicy.RUNTIME:运行时有效

3.编写拦截器

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @ClassName: ApiAuthAspect
 * @Description: 自定义拦截器
 * @Author: luzihao
 * @Email: 458243101@qq.com
 * @Date: 2020/9/23 9:53
 */
@Aspect
@Component
public class ApiAuthAspect {

    //切点
    @Pointcut("@annotation(com.你的注解路径.annotation.ApiAuth) || @within(com.你的注解路径.annotation.ApiAuth)")
    public void cut() {}

    //前置
    @Before("cut()")
    public void beforePointcut(JoinPoint joinPoint) {
        System.out.println(joinPoint.getArgs()[0]); //所有参数,这里输出第一个
        System.out.println(joinPoint.getSignature()); //拦截到的方法信息
        System.out.println(joinPoint.getTarget()); //被代理对象
        System.out.println(joinPoint.getThis()); //代理对象
    }
    
    //后置
    @After("cut()")
    public void afterPointcut(JoinPoint joinPoint) {}
    
    //环绕
    @Around("cut()")
    public void aroundPointcut(JoinPoint joinPoint) {}
    
    //返回之后
    @AfterReturning("cut()")
    public void afterReturningPointcut(JoinPoint joinPoint) {}
    
    //报错时
    @AfterThrowing("cut()")
    public void afterThrowingPointcut(JoinPoint joinPoint) {}
}

@Aspect:标注当前类为切面容器

4.使用

  • 编写测试Controller
import com.你的注解路径.annotation.ApiAuth;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;

//用于测试注解
@ApiAuth //可直接在类上使用,亦可在方法上
@Controller
@RequestMapping("/test")
public class TestController {

    @GetMapping
    public String test(String s) {
        return "hello";
    }
}
  • 浏览器访问

在这里插入图片描述

  • 控制台输出
444
String com.test.controller.TestController.test(String)
com.test.controller.TestController@44b58a5e
com.test.controller.TestController@44b58a5e

未经允许,请勿转载!

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java自定义注解是指在Java语言可以通过编写代码来定义自己的注解。自定义注解可以提供一些额外的元数据信息,用于标记和描述Java代码的某个元素。自定义注解可以用于方法、属性等各个层面。 实现自定义注解步骤如下: 1. 使用@Retention注解指定注解的保留策略,默认为RetentionPolicy.CLASS。可选的保留策略有三种:RetentionPolicy.SOURCE、RetentionPolicy.CLASS和RetentionPolicy.RUNTIME。 2. 使用@Target注解指定注解的作用目标,默认可以用于所有的Java元素。可选的作用目标包括ElementType.TYPE(、接口、枚举等)、ElementType.FIELD(字段、枚举常量等)、ElementType.METHOD(方法)、ElementType.PARAMETER(方法参数)、ElementType.CONSTRUCTOR(构造方法)、ElementType.LOCAL_VARIABLE(局部变量)等。 3. 使用@interface关键字定义注解,并定义注解的属性。注解的属性以无参无异常抛出的方法的形式定义,可以指定默认值。 4. 在需要使用注解的地方使用自定义注解自定义注解可以携带信息,这些信息可以在运行时通过反射获取,对注解进行解析和处理自定义注解可以用于编写各种工具、框架和库,来增强程序的扩展性和灵活性。 实现自定义注解一个典型应用场景是在Spring框架的依赖注入(DI)和面向切面编程(AOP。通过自定义注解,可以标记需要注入的Bean,或者标记需要进行切面拦截方法,从而实现依赖注入和切面编程的功能。 总的来说,Java自定义注解Java语言提供的一种灵活的元编程机制,可以通过注解增加程序的可读性和可维护性,同时也可以用于实现一些特定的功能,如依赖注入和切面编程等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白给卢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值