自定义注解 参数判空(注解+AOP)

自定义注解 参数判空(注解+AOP)

(AOP相关依赖需要在pom文件中自行导入)

一、创建自定义注解 CheckNull

1、新建 @interface 文件 checkNull (可以先创建.java文件后将class文件改成@interface即可)

2、关于@Retention、@Documented、@Target详情去看 元注解。 传送门:https://blog.csdn.net/liang100k/article/details/79515910

package org.jeecg.modules.portal.anno;

import java.lang.annotation.*;

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

}

二、创建切面(AOP)

1、加上@Aspect

2、定义切点@Pointcut(”@annotation(注解路径)“)

​ @annotation可以理解为切点在一个注解上

​ @Before 前置增强

​ @Around 环绕增强

​ @after 后置增强

​ 具体去看相关文章:https://blog.csdn.net/zhanglf02/article/details/78132304

3.在环绕增强中写判空的逻辑( Result 为自己封装的统一返回结果,也可以直接抛出一个异常)

附源码:

package org.jeecg.modules.portal.aop;

import cn.hutool.core.util.StrUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.jeecg.common.api.vo.Result;
import org.springframework.stereotype.Component;

@Aspect
@Component
@SuppressWarnings({"unused"})
public class MyAspect {
    @Pointcut("@annotation(org.jeecg.modules.portal.anno.CheckNull)") //自定义切点-->(作用于哪里)
    public void annotationPointcut() {
    }

    @Before("annotationPointcut()") //进入方法前的操作
    public void before(JoinPoint joinPoint) {

    }

    @Around("annotationPointcut()")
    public Object round(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();

        String[] params = signature.getParameterNames();//获取所有的参数名称
        Object[] args = joinPoint.getArgs();//获取所有的参数值

        int length = args.length;

        for (int i = 0; i <length ; i++) {
            String s = args[i].toString();
            if(StrUtil.isEmpty(s)){
                String msg = "参数"+params[i]+"不能为空";
                return Result.error(msg);
            }
        }

        return joinPoint.proceed();
    }

}

三、实例测试

1、在控制器上加上创建好的注解 @CheckNull

    /**
     *   测试用的接口
     *
     */
    @ApiOperation(value="测试接口", notes="测试接口")
    @PutMapping(value = "/testA")
    @CheckNull
    public Result<String> testA(@RequestParam(name = "a",required = false) String a,
                                @RequestParam(name = "b",required = false)String b) {
        return Result.OK("");
    }

测试结果:

在这里插入图片描述
小白。。 功能还待完善。。

  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 15
    评论
Java中,自定义注解(Annotation)是一种元数据,用于在代码中提供附加信息,而这些信息通常不会直接影响程序的执行。自定义注解实体类是一种用户创建的注解类型,它包含一个或多个字段,这些字段可以存储额外的数据。 要创建一个自定义注解,你需要按照以下步骤操作: 1. 定义注解类:创建一个新的Java接口,添加你想要的属性(@Retention, @Target, 或者你自己定义的字段),例如: ```java 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(); int priority() default 0; } ``` 这里定义了一个名为`MyCustomAnnotation`的注解,有两个属性:`value`和`priority`。 2. 在目标类中使用注解:在需要应用注解的方法上添加注解,例如: ```java public class MyClass { @MyCustomAnnotation(value = "Important Method", priority = 1) public void myMethod() { // 方法体... } } ``` 3. AOP(面向切面编程)与自定义注解:Spring AOP或AspectJ等工具可以让你在运行时扫描并处理带有特定注解的方法。你可以编写一个切面(Aspect),使用`@Around`, `@Before`, 或 `@After` 等通知处理`@MyCustomAnnotation`注解的方法。例如,你可能想在方法执行前后执行一些额外的操作,或者根据`priority`属性决定是否执行某些操作。 ```java @Aspect @Component public class MyAspect { @Around("@annotation(myCustomAnnotation)") public Object advice(ProceedingJoinPoint joinPoint, MyCustomAnnotation myCustomAnnotation) throws Throwable { System.out.println("Before executing method with priority: " + myCustomAnnotation.priority); Object result = joinPoint.proceed(); System.out.println("After executing method with priority: " + myCustomAnnotation.priority); return result; } } ```
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值