zbb20181224 java,annotation 注解,自定义注解解析2

 

InitField.java

package com.zbb.app.annotation;

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

@Documented
@Inherited
@Target({ ElementType.FIELD, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface InitField {
     public String value() default "";
}


Test.java

package com.zbb.app.annotation;

import java.lang.reflect.InvocationTargetException;

public class Test {
     public static void main(String[] args)
             throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
         User user = UserFactory.create();

//        User user = new User();
         System.out.println(user.getName());
         System.out.println(user.getAge());
     }
}


User.java

package com.zbb.app.annotation;

public class User {
     private String name;
     private String age;

    public String getName() {
         return name;
     }

    @InitField(value = "zbb")
     public void setName(String name) {
         this.name = name;
     }

    public String getAge() {
         return age;
     }

    @InitField(value = "123")
     public void setAge(String age) {
         this.age = age;
     }
}

posted @ 2018-12-24 10:22  DaryllD  阅读(121)  评论(0编辑  收藏  举报