自定义注解为属性赋值
本文将介绍如何使用自定义注解加载配置文件中的信息给类中的属性赋值
- 定义注解
-
定义@LoadProperty注解,用来加载配置文件
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; /** * @description 加载配置文件,value传入文件路径,与@{ConfigField}配套使用 * @author xujiali * @date 2019-09-12 4:03:22 PM */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface LoadProperty { String value(); //配置文件(.properties)路径 }
-
定义@ConfigField注解,用来给属性赋值
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; /** * @description 给属性赋值,与@{LoadProperty}配套使用 * @author xujiali * @date 2019-09-12 4:00:15 PM */ @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ConfigField { String value(); //.properties中的key }
- 通过反射对注解进行解析
import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; import java.util.Properties; public class AnnoResolve { public static <T> void loadProperty(T t) { Class<? extends Object> cls = t.getClass(); boolean hasLoadPropertyAnno = cls.isAnnotationPresent(LoadProperty.class); if (hasLoadPropertyAnno) { //为属性赋值 configField(cls, t); } } private static <T> void configField(Class<? extends Object> cls, T t) { String filePath = cls.getAnnotation(LoadProperty.class).value(); Properties properties = new Properties(); InputStream is = AnnoResolve.class.getClassLoader().getResourceAsStream(filePath); try { properties.load(is); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { boolean hasConfigField = field.isAnnotationPresent(ConfigField.class); String fieldValue = null; if (hasConfigField) { //若属性上有注解,使用注解的值作为key去配置文件中查找 //获取注解的值 Object annoValue = field.getAnnotation(ConfigField.class).value(); fieldValue = properties.getProperty(annoValue.toString()); } else { //若属性上没有注解,则使用属性名作为key去配置文件中查找 fieldValue = properties.getProperty(field.getName()); } field.setAccessible(true); field.set(t, fieldValue); } } catch (IOException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } }
- 使用注解
-
创建配置文件config.properties,声明属性值(key=value)
user.id=111 name=zhangsan
-
在类中使用注解
@LoadProperty("config.properties") public class User { @ConfigField("user.id") private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
- 测试
public class Test { public static void main(String[] args) { User user = new User(); AnnoResolve.loadProperty(user); System.out.println(user.getId()); System.out.println(user.getName()); } }