java自定义注解,结合实际业务编写demo

1.业务场景

前端访问后端,部分接口需要先进行判断。可以把当前需要判断的代码做成一个注解。
例如:
盘点期间,不允许责任人,保管员等的变动。多个地方都要对当前是否有盘点任务进行判断。

2.自定义注解

1.定义注解

/**
 * 自定义注解
 */

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

2.自定义异常

public class LockException  extends RuntimeException  {
    private static final long serialVersionUID = 1L;
    
    private String code;
    private String msg;

    public LockException(String code,String msg) {
        this.code = code;
        this.msg=msg;
    }


    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

3.定义切面

@Component
@Aspect
public class Validation {
    @Before("@annotation(com.example.onemodule.aop.TestTask)")
    public void CheckEndTask(){
        //模拟查询业务,1:有业务 0:无业务
        int rd= Math.random()>0.5?1:0;
        if(rd==1){
            throw new LockException("333","主动抛出来一个异常,任务未结束");
        }
    }

}

4.捕获异常

@RestControllerAdvice
public class ExceptionControllerHandler {

    @ExceptionHandler(value = Exception.class)
    public String handler(Exception ex) {
        
        if (ex instanceof LockException) {
           return "接到——————333主动抛出来一个异常,任务未结束";
        }
        return "22222222222";
    }
}

5.使用

@RestController
@RequestMapping("/hello")
public class Hello {

    @TestTask
    @RequestMapping()
    public String hello(){
        return "我是张三";
    }
}

6.结果
6.1正常访问
在这里插入图片描述
6.2拦截提醒

在这里插入图片描述

3.元注解

@Target :用于描述注解的使用范围

public enum ElementType {
    /** 
    * Class, interface (including annotation type), or enum declaration
    * 类、接口(包括注释类型)或枚举声明
    */
    TYPE,

    /** 
    * Field declaration (includes enum constants)
    * 字段声明(包括枚举常量)
    */
    FIELD,

    /** 
    * Method declaration
    * 方法声明
    */
    METHOD,

    /** 
    * Formal parameter declaration 
	* 形式参数声明
	*/
    PARAMETER,

    /** 
    * Constructor declaration
	* 构造函数声明
	*/
    CONSTRUCTOR,

    /** 
    * Local variable declaration
    * 局部变量声明
    */
    LOCAL_VARIABLE,

    /** 
    * Annotation type declaration
    * 注释类型声明
    */
    ANNOTATION_TYPE,

    /** 
    * Package declaration
    * 包声明
    */
    PACKAGE,

    /**
     * Type parameter declaration
     * 类型参数声明
     * @since 1.8
     */
    TYPE_PARAMETER,

    /**
     * Use of a type
     * 类型的使用
     * @since 1.8
     */
    TYPE_USE
}

@Retention:被描述的注解在什么范围内有效

public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     * 注释将被编译器丢弃。
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     * 注释将由编译器记录在类文件中但不需要在运行时被VM保留。这是默认值行为。
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     * 注释将由编译器记录在类文件中,并且在运行时被VM保留,因此可以反射性地读取它们。
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

@Documented

/**
 * Indicates that annotations with a type are to be documented by javadoc
 * and similar tools by default.  This type should be used to annotate the
 * declarations of types whose annotations affect the use of annotated
 * elements by their clients.  If a type declaration is annotated with
 * Documented, its annotations become part of the public API
 * of the annotated elements.
 *指示类型为的注释将由javadoc记录以及默认的类似工具。此类型应用于注释类型的声明,其注释会影响带注释的用
 *客户提供的元素。如果类型声明用在文档中,它的注释成为公共API的一部分注释元素的。
 * @author  Joshua Bloch
 * @since 1.5
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}

@Inherited

/**
 * Indicates that an annotation type is automatically inherited.  If
 * an Inherited meta-annotation is present on an annotation type
 * declaration, and the user queries the annotation type on a class
 * declaration, and the class declaration has no annotation for this type,
 * then the class's superclass will automatically be queried for the
 * annotation type.  This process will be repeated until an annotation for this
 * type is found, or the top of the class hierarchy (Object)
 * is reached.  If no superclass has an annotation for this type, then
 * the query will indicate that the class in question has no such annotation.
 *
 * <p>Note that this meta-annotation type has no effect if the annotated
 * type is used to annotate anything other than a class.  Note also
 * that this meta-annotation only causes annotations to be inherited
 * from superclasses; annotations on implemented interfaces have no
 * effect.
 *
 * @author  Joshua Bloch
 * @since 1.5
 * @jls 9.6.3.3 @Inherited
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值