Java 反射将配置文件数据加载到对象属性中Reflect与Properties使用
2017-12-01 18:16
936 查看
Java 反射将配置文件数据加载到对象属性中Reflect与Properties使用
Java 反射 可以根据类名找到相应的类,也可以将配置文件中的值加载到对应属性中。需要用到的包:spring-core-3.1.2.Release.jar
Java 反射的一种应用:
import java.io.File; import java.io.IOException; import java.lang.reflect.Field; import java.util.Properties; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.support.PropertiesLoaderUtils; /** * 类说明 * * <pre> * Modify Information: * Author Date Description * ============ =========== ============================ * DELL 2017年4月25日 Create this file * </pre> * */ public class Reflect4Proterties { public static final String APP_CONFIG_FILE = "application.properties"; public static int batchUpdateSize = 100; /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { String configPath ="D:/CPCN/Payment/StatementExternal/config"; FileSystemResource resource = new FileSystemResource(configPath + File.separatorChar + APP_CONFIG_FILE); Properties configProperties = new Properties(); PropertiesLoaderUtils.fillProperties(configProperties, resource); try { reflectFieldValue("StatExternal", Reflect4Proterties.class, configProperties, null); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void reflectFieldValue(String preFix, Class<?> reflectClass, Properties properties, Object obj) throws Exception { Field[] allFields = reflectClass.getDeclaredFields(); Field thisField = null; String fieldName = ""; String fieldValue = ""; String preFixStr = isNotEmpty(preFix) ? (preFix + ".") : ""; // 如果有前缀,则是前缀加上.如前缀BANK_B2C_104,则最后去相应properties中的值为BANK_B2C_104.(fileldName) for (int i = 0; i < allFields.length; i++) { thisField = allFields[i]; fieldName = thisField.getName(); fieldName = preFixStr + fieldName; fieldValue = (String) properties.get(fieldName); // 此处只能用null,不能用"",因为bank.properties中可能有空的值 if (null != fieldValue) { if ("int".equals(thisField.getType().getName())) { thisField.set(obj, Integer.parseInt(fieldValue)); } else if ("boolean".equals(thisField.getType().getName())) { thisField.set(obj, Boolean.parseBoolean(fieldValue)); } else if ("long".equals(thisField.getType().getName())) { thisField.set(obj, Long.parseLong(fieldValue)); } else { thisField.set(obj, fieldValue.trim()); } System.out.println("---注入" + fieldName + "的值为\"" + fieldValue + "\"成功---"); } } } /** * 判断字符串是否不为空 */ public static boolean isNotEmpty(String str) { return str != null && !"".equals(str.trim()); } }
配置文件:
相关文章推荐
- log4j的使用与java中properties配置文件加载
- 反射的应用,读取properties配置文件中的数据(普通InputStream方式,类加载方式),再调用运行
- java 加载properties配置文件属性到常量
- java反射,类加载器以及配置文件的结合使用
- 问题:使用java中properties加载配置文件
- log4j的使用与java中properties配置文件加载
- java反射调用配置文件构造类对象,使用其方法
- java中使用Properties加载XML文件设置java窗体应用程序的窗体属性
- ArrayList 与HashSet的比较,及应用反射读取properties配置文件中的数据进行实例化再调用,以及类加载器的使用;还有HashCode的分析,及导致内存泄露,内存溢出的原因之一
- 编写一个文件,读取 src 下面day12包 下面的my.properties 文件,文件内容如下, className=day12.User 结合 IO、反射知识,完成 配置文件中对象的创建和方法调用。并编写方法,列举出 Student 类中所有的属性、
- 如何使用java类来加载properties配置文件的属性信息
- java 从属性文件加载数据的方法工具类 实例 可直接使用
- java加载配置文件properties中的数据
- web工程中普通 java 类使用 properties 配置文件中属性
- java反射调用配置文件构造类对象,使用其方法
- java中经常用到的读写配置文件的信息properties属性的写入,读取例子
- 使用反射机制动态加载配置文件
- Java中封装配置文件(1)-使用Properties
- java 高新技术【6】 反射开发框架以及用 类加载器 管理资源和配置文件
- Android 在Java代码中设置style属性--使用代码创建ProgressBar对象(反射机制)