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

 

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

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());
    }

}

  

 配置文件:

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

#每次批量更新条数上限
StatExternal.batchUpdateSize=100

  

转载于:https://www.cnblogs.com/wangming2011hit/p/6760974.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值