日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

Java 反射将配置文件数据加载到对象属性中

發(fā)布時(shí)間:2025/6/15 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java 反射将配置文件数据加载到对象属性中 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

Java 反射將配置文件數(shù)據(jù)加載到對(duì)象屬性中

Java 反射 可以根據(jù)類名找到相應(yīng)的類,也可以將配置文件中的值加載到對(duì)應(yīng)屬性中。

需要用到的包:spring-core-3.1.2.Release.jar

Java 反射的一種應(yīng)用:

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;/*** 類說(shuō)明* * <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 blocke.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,則最后去相應(yīng)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,不能用"",因?yàn)閎ank.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());}}

  

?配置文件:

D:/CPCN/Payment/StatementExternal/config/application.properties

#每次批量更新條數(shù)上限 StatExternal.batchUpdateSize=100

  

轉(zhuǎn)載于:https://www.cnblogs.com/wangming2011hit/p/6760974.html

總結(jié)

以上是生活随笔為你收集整理的Java 反射将配置文件数据加载到对象属性中的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。