zoukankan      html  css  js  c++  java
  • Spring Boot中自定义注解

    1、首先我们了解一下如何自定义一个注解。

          @Target(),下面是@Target的描述

     * The constants of this enumerated type provide a simple classification of the
     * syntactic locations where annotations may appear in a Java program. These
     * constants are used in {@link Target java.lang.annotation.Target}
     * meta-annotations to specify where it is legal to write annotations of a
     * given type.
    

      可以看到它主要用来表面注解用在哪里,我们常常可以看到有的注解用于类上,有的用于方法上等等,这个就是来表面注解用的位置的。

          它的参数有下面几个类型:

    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,同样可以用上面的方法,可以看到它主要用来表面注解保留到什么时候的。

    主要参数有:

    RetentionPolicy.RUNTIME : 在.class文件中仍保留,在虚拟机运行时也保留着。

    RetentionPolicy.CLASS : 保留在.class文件中,但是在虚拟机运行时将会删除,默认这个。

    RetentionPolicy.SOURCE: 在编译器生成.class文件过程中就会丢弃。

    下面自定义一个注解Log,在使用时就可以@Log(value="");默认为空,Log也可以加其他的方法。

    package com.springboot.annotation;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Log {
        String value() default "";
    }
  • 相关阅读:
    团队项目:第二次冲刺站立会议06
    书店促销
    团队项目:第二次冲刺站立会议05
    团队项目:第二次冲刺站立会议04
    学习进度条(第十三周)
    团队项目:第二次冲刺站立会议03
    团队项目:第二次冲刺站立会议02
    寻找小水王
    团队项目:第二次冲刺站立会议01
    FusionNet: A deep fully residual convolutional neural network for image segmentation in connectomics
  • 原文地址:https://www.cnblogs.com/minblog/p/12547557.html
Copyright © 2011-2022 走看看