zoukankan      html  css  js  c++  java
  • Java自定义注解

    元注解:注解的注解。

    项目结构

    定义FruitName注解

    package com.annotation;
    
    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;
    
    /*
     * 水果名称注解
     */
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface FruitName {
        String value() default "";
    }

    定义FruitColor注解

    package com.annotation;
    
    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;
    
    /*
     * 水果颜色注解
     */
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface FruitColor {
        public enum Color {红色,绿色,蓝色};
        Color color() default Color.红色;
    }

    定义FruitProvider注解

    package com.annotation;
    
    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;
    
    
    /*
     * 水果供应商注解
     */
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface FruitProvider {
        String name() default "";
        String address() default "";
    }

    定义Apple类

    package com.fruit;
    
    import com.annotation.FruitColor;
    import com.annotation.FruitName;
    import com.annotation.FruitProvider;
    import com.annotation.FruitColor.Color;
    
    public class Apple {
        @FruitName("红富士")
        private String name;
        @FruitColor(color=Color.红色)
        private String color;
        @FruitProvider(name="烟台果品",address="烟台市芝罘区幸福中路")
        private String provider;
    }

    定义Test类

    package com.test;
    
    import java.lang.reflect.Field;
    
    import com.annotation.FruitColor;
    import com.annotation.FruitName;
    import com.annotation.FruitProvider;
    import com.fruit.Apple;
    
    public class Test {
        public static void main(String[] args) {
            Field[] fields = Apple.class.getDeclaredFields();
            for(Field field : fields){
                if(field.isAnnotationPresent(FruitName.class)){
                    FruitName fruitName = field.getAnnotation(FruitName.class);
                    System.out.println("水果名称:"+fruitName.value());
                }else if(field.isAnnotationPresent(FruitColor.class)){
                    FruitColor fruitColor = field.getAnnotation(FruitColor.class);
                    System.out.println("水果颜色:"+fruitColor.color());
                }else if(field.isAnnotationPresent(FruitProvider.class)){
                    FruitProvider provider = field.getAnnotation(FruitProvider.class);
                    System.out.println("供货商:"+provider.name()+"("+provider.address()+")");
                }
            }
        }
    }

    运行结果

    水果名称:红富士
    水果颜色:红色
    供货商:烟台果品(烟台市芝罘区幸福中路)
  • 相关阅读:
    easyui struts后台实现tree返回json数据
    jquery扩展方法
    EasyUI tree扩展获取实心节点
    Hibernate之AbstractEntityPersister
    No CurrentSessionContext configured 异常解决
    Dubbo远程调用服务框架原理与示例
    mongodb高级操作及在Java企业级开发中的应用
    Java和MongoDB之Hello World
    Vmware 虚拟的Linux系统如何与宿主主机共享上网
    VM VirtualBox 上安装 CentOs6.4(详细)
  • 原文地址:https://www.cnblogs.com/sea-breeze/p/6972910.html
Copyright © 2011-2022 走看看