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 "";
    }
  • 相关阅读:
    关于silverlight打印模糊的问题
    Microsoft Office Excel 不能访问文件及COM无法访问
    IE8 下 select option 内容过长 , 展开时信息显示不全解决办法
    如何用css做一个细虚线边框表格
    DIV 垂直 垂直水平 居中
    Ul li 横排 菜单
    对原生js的一些小尝试
    Nodejs学习笔记——Assert(断言)
    Octopress创建GitHub Pages——基于代码托管的静态博客
    JS倒计时器一只,顺便复习javascript时间相关函数
  • 原文地址:https://www.cnblogs.com/minblog/p/12547557.html
Copyright © 2011-2022 走看看