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 "";
    }
  • 相关阅读:
    iOS的生命周期
    【iOS开发】在 App 中加入 AdMob 广告
    iOS性能优化:Instruments
    动态的计算行高 加载数据源 有多少显示多少 tableView 包含 colloctionView 显示复杂的界面写法
    Java String.split()注意点
    Java eclipse export jar包 包括第三方引入的jar
    微信网页授权获取用户基本信息
    android连接本地tomcat服务器,报timeout
    iOS内存管理retain,assign,copy,strong,weak
    static与全局与局部变量的区别
  • 原文地址:https://www.cnblogs.com/minblog/p/12547557.html
Copyright © 2011-2022 走看看