ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

自定义注解为属性赋值

2022-07-04 01:03:09  阅读:141  来源: 互联网

标签:lang java String 自定义 annotation import 注解 public 赋值


本文将介绍如何使用自定义注解加载配置文件中的信息给类中的属性赋值

  1. 定义注解
  • 定义@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
}
  1. 通过反射对注解进行解析
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();
			}
		}
		
	}
}
  1. 使用注解
  • 创建配置文件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;
	}
}
  1. 测试
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());
	}
}
来源:https://blog.csdn.net/xxujia/article/details/100879992

标签:lang,java,String,自定义,annotation,import,注解,public,赋值
来源: https://www.cnblogs.com/konglxblog/p/16441439.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有