Java 中自定义注解

package com.learn.map;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

/**

* @Description: 用户全名

* @Author Administrator

* @Date 2020/7/8 10:39

* @Version 1.0

*/

@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})

@Retention(RetentionPolicy.RUNTIME)

public @interface FullName {

String values()default "";

}

package com.learn.map;

import java.io.Serializable;

/**

* @Description: 用户对象

* @Author Administrator

* @Date 2020/7/8 10:41

* @Version 1.0

*/

@FullName(values ="用户对象")

public class UserInfo implements Serializable {

@FullName(values ="用户ID")

private String id;

@FullName(values ="用户姓名")

private String name;

@FullName(values ="获取用户名称")

public String getName() {

return name;

}

@FullName(values ="设置用户名称")

public void setName(String name) {

this.name =name;

}

@FullName(values ="获取用户ID")

public String getId() {

return id;

}

@FullName(values ="设置用户ID")

public void setId(String id) {

this.id =id;

}

}

FullName fullName =UserInfo.class.getAnnotation(FullName.class);

System.out.println(fullName.values());

Field[]fields =UserInfo.class.getDeclaredFields();

for (Field field:fields) {

fullName =field.getAnnotation(FullName.class);

System.out.println(fullName.values());

}

Method[]methods =UserInfo.class.getDeclaredMethods();

for (Method method:methods) {

fullName =method.getAnnotation(FullName.class);

System.out.println(fullName.values());

}

你可能感兴趣的:(Java 中自定义注解)