自定义注解实战

注解属性

1.1 @Target 表示该注解目标(作用范围),可能的 ElemenetType 参数包括:  
    ElemenetType.CONSTRUCTOR 构造器声明  
    ElemenetType.FIELD 域声明(包括 enum 实例)   
    ElemenetType.LOCAL_VARIABLE 局部变量声明   
    ElemenetType.METHOD 方法声明   
    ElemenetType.PACKAGE 包声明   
    ElemenetType.PARAMETER 参数声明   
    ElemenetType.TYPE 类,接口(包括注解类型)或enum声明  

1.2 @Retention 表示该注解的生命周期,可选的 RetentionPolicy 参数包括  
    RetentionPolicy.SOURCE 注解将被编译器丢弃   
    RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃   
    RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息  

1.3 @Documented 指示将此注解包含在 javadoc 中  
1.4 @Inherited 指示允许子类继承父类中的注解  

2.自定义类注解

package com.xx.xx.web.common.myannotation;  

import java.lang.annotation.Documented;  
import java.lang.annotation.ElementType;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
import java.lang.annotation.Target;  

/** 
 * 描述:标注在类上面自定义注解 
 * 
 * @author  
 * @create_time  
 */  
@Target(ElementType.TYPE)  
@Retention(RetentionPolicy.RUNTIME)  
@Documented  
public @interface MyClassAnnotation {  

    // 系统名称  
    String sysName() default "";  

    // 唯一标识id  
    int id();  

    // 操作描述  
    String desc() default "";  

}  

3.自定义方法注解

package com.xx.xx.web.common.myannotation;  

import java.lang.annotation.Documented;  
import java.lang.annotation.ElementType;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
import java.lang.annotation.Target;  

/** 
 * 描述:标注在方法上面的自定义注解 
 * 
 * @author  
 * @create_time  
 */  
@Target(ElementType.METHOD)  
@Retention(RetentionPolicy.RUNTIME)  
@Documented  
public @interface MyMethodAnnotation {  

    String methodDesc() default "该方法没有描述";  

}  

4.自定义属性字段注解


package com.xx.xx.web.common.myannotation;  

import java.lang.annotation.ElementType;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
import java.lang.annotation.Target;  

/** 
 * 描述:标注在字段、属性上面的自定义注解 
 * 
 * @author  
 * @create_time  
 */  
@Target(ElementType.FIELD)  
@Retention(RetentionPolicy.RUNTIME)  
public @interface MyFieldAnnotation {  

    String name() default "";  
}  

使用注解的类:

package com.xx.xx.web.common.myannotation;  

/** 
 * 描述:自定义注解运用类 
 * 
 * @author  
 * @create_time  
 */  
@MyClassAnnotation(sysName = "电商平台",id = 10,desc = "类注解")  
public class TestAnnotation {  

    @MyFieldAnnotation(name = "字段名称")  
    private String name;  

    private int age;//无注解  

    private String address;//无注解  

    @MyMethodAnnotation(methodDesc = "getElement方法")  
    public String getElement() {  
        return  name;  
    }  

    // 无方法注解  无实际意义  
    public int getAgeElement() {  
        return  age;  
    }  

}  

测试执行类:

import java.lang.reflect.Field;  
import java.lang.reflect.Method;  

/** 
 * 描述:自定义注解执行测试类 
 * 
 * @author  
 * @create_time  
 */  
public class ExecuteAnnotation {  

    public static void main(String[] args) {  

        // 获取类注解  
        MyClassAnnotation myClassAnnotation = TestAnnotation.class.getAnnotation(MyClassAnnotation.class);  

        if (myClassAnnotation == null) return;  

        System.out.println("id = " + myClassAnnotation.id() + "&sysName=" + myClassAnnotation.sysName() + "&desc=" + myClassAnnotation.desc());  

        System.out.println("**********************类注解end****************************");  
        System.out.println();  

        // 获取方法注解  
        TestAnnotation testAnnotation = new TestAnnotation();  
        Method[] methods = testAnnotation.getClass().getDeclaredMethods();  

        for (Method method: methods) {  
            System.out.println(method.getName());  

            // 获取该方法的解决  
            MyMethodAnnotation myMethodAnnotation = method.getAnnotation(MyMethodAnnotation.class);  

            // 无方法注解的话跳过  
            if (myMethodAnnotation == null) continue;  

            System.out.println("methodName = " + method.getName() + "&methodDesc=" + myMethodAnnotation.methodDesc());  

        }  

        System.out.println("**********************方法注解end****************************");  
        System.out.println();  

        // 获取字段注解  
        Field[] fields = TestAnnotation.class.getDeclaredFields();  
        for (Field field: fields) {  

            System.out.println(field.getName());  

            // 获取该字段的解决  
            MyFieldAnnotation myFieldAnnotation = field.getAnnotation(MyFieldAnnotation.class);  

            if (myFieldAnnotation == null) continue;  

            System.out.println("fieldName=" + field.getName() + "&name=" + myFieldAnnotation.name());  

        }  

        System.out.println("**********************字段注解end****************************");  

    }  

}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值