1. 程式人生 > 實用技巧 >kubernets-java 動態修改deployment 的replicas

kubernets-java 動態修改deployment 的replicas

註解的分類

  1.標準註解

  2.元註解

    @Retention :註解的生命週期

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    /**
     * Returns the retention policy.
     * @return the retention policy
     */
    RetentionPolicy value();
}

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. */ 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. * * @see java.lang.reflect.AnnotatedElement
*/ RUNTIME }

SOURCE:只作用在java類中,.class裡沒有這個註解

.CLASS:在.class裡也會有這個註解

.RUNTIME:在執行時這個註解也會起到作用.

    @Target :註解的作用目標

      

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    /**
     * Returns an array of the kinds of elements an annotation type
     * can be applied to.
     * @return an array of the kinds of elements an annotation type
     * can be applied to
     
*/ ElementType[] value(); }

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
}

 TYPE:作用在類上

 Field:作用在屬性上

 Method:作用在方法上

    @Inherited :是否允許之類繼承該註解

    @Documented

自定義註解的實現

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface PersonAnnotation {
    String name();
    int age();
    String gender() default  "";
    String[] language();
}

獲取註解的資訊

public static void main(String[] args) throws Exception {
        Class clazz = Class.forName("Annotation.Person");
        PersonAnnotation annotation = (PersonAnnotation) clazz.getAnnotation(PersonAnnotation.class);
        System.out.println(annotation.age()+"\n"+annotation.gender()+"\n"+annotation.name());
        for(String str : annotation.language()){
            System.out.println(str+" ");
        }
    }

通過反射可以獲取到註解的資訊,但前提是註解的生命週期必須是Runntime.