最简单的springboot自定义注解

本篇是自己在写自定义注解的时候遇到一些问题,故写下一个最简单就生效的案例。

首先定义一个自己的注解类:

package com.shenjian.user.annotation;

import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
public @interface CheckAuth {

}

第一个注解@Documented 可以省略,其它两个必须要有。

然后再定义一个切面方法来使你的注解类可以被使用:

package com.shenjian.user.Aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class CheckAuth {

    private static Logger logger = LoggerFactory.getLogger(CheckAuth.class);

    @Pointcut("@annotation(com.shenjian.user.annotation.CheckAuth)")
    public void checkAuthAnnotation(){
        logger.info("this is checkAuthAnnotation.........");
    }

    @Before("checkAuthAnnotation()")
    public void beforeCut(JoinPoint joinPoint){
        logger.info("this is beforeCut.............");
    }

    @After("checkAuthAnnotation()")
    public void afterCut(){
        logger.info("this is afterCut.............");
        System.out.println("????");
    }

    @Around("checkAuthAnnotation()")
    public Object aroundCut(ProceedingJoinPoint joinPoint){
        logger.info("this is aroundCut.............");
        Object result = null;
        try {
            logger.info("this is before proceed.............");
            result = joinPoint.proceed();
            logger.info("this is after proceed.............");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return result;
    }

    @AfterReturning("checkAuthAnnotation()")
    public void afterReturnCut(JoinPoint joinPoint){
        logger.info("this is afterReturnCut.............");
    }

    @AfterThrowing(value = "checkAuthAnnotation()",throwing = "e")
    public void afterThrowCut(JoinPoint joinPoint,Exception e){
        logger.info("this is afterThrowCut.............");
    }


}

 首先两个注解不能漏了

其次 

@Pointcut("@annotation(com.shenjian.user.annotation.CheckAuth)") 是指向你刚刚创建的自定义注解的。这个在现在可以直接使用方法名checkAuthAnnotation() 来作为参数使用。

然后一定要注意的是 @Around("checkAuthAnnotation()") 这个Around是最麻烦的。首先,它的参数和其它方法就不一样,它是 ProceedingJoinPoint ,因为要调用它的process方法。普通的JoinPoint 是没有这个方法的。其次这个类一定要有返回值。之前我看网上有些文章是没有返回值的,所以我也就没写。但是运行的使用一直会卡住,找了好久才发现是这个方法没返回。(之前方法返回设置的是void所以编译没问题)。

最后还有个要注意的就是 @AfterThrowing(value = "checkAuthAnnotation()",throwing = "e")

这个一定要加上 throwing = "e",和 public void afterThrowCut(JoinPoint joinPoint,Exception e)是配对的,不然就会报错。

定义好这两个配置,接下来就是使用了。只需要在你的controller的方法上加上你的注解就可以了

我定义的注解名是 CheckAuth,所以只需要加上@CheckAuth

下面看下实际效果。

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于SpringBoot定义注解AOP的问题,我可以为您提供一些基本的介绍和示例代码。 首先,AOP(Aspect-Oriented Programming)是一种编程范式,它可以在不修改业务逻辑代码的情况下,对应用程序进行横切关注点的切面处理。而Spring AOP作为Spring框架的一部分,提供了一种基于代理模式的AOP实现。 在使用Spring AOP的过程中,自定义注解可以作为切点表达式的一部分,通过对注解的解析,实现对被注解的方法或类的切面处理。下面是一个简单的示例代码,演示如何通过自定义注解实现对方法的AOP处理: 首先,定义一个自定义注解: ```java @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation { String value() default ""; } ``` 然后,在需要被拦截的方法上添加该注解: ```java @Service public class MyService { @MyAnnotation("myAnnotation") public void doSomething() { System.out.println("do something..."); } } ``` 接下来,使用AspectJ的@Aspect注解定义一个切面类,并在该类中定义一个切点,用于匹配被@MyAnnotation注解的方法: ```java @Aspect @Component public class MyAspect { @Pointcut("@annotation(com.example.demo.annotation.MyAnnotation)") public void myAnnotationPointcut() {} @Before("myAnnotationPointcut()") public void beforeMyAnnotation() { System.out.println("before myAnnotation..."); } } ``` 最后,启动SpringBoot应用程序,调用MyService的doSomething方法,就可以看到输出结果: ```java before myAnnotation... do something... ``` 以上就是一个简单SpringBoot定义注解AOP的示例。通过使用自定义注解,可以更加方便地实现对应用程序的切面处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值