動態讀取配置文件給類中靜態變量賦值

# 使用註解或其餘靜態注入的方法 這裏不討論

相關文章:https://blog.csdn.net/5iasp/article/details/46652115
https://blog.csdn.net/z69183787/article/details/39343259 Java Field 詳解
https://www.cnblogs.com/gmq-sh/p/5942065.html java獲取對象屬性類型、屬性名稱、屬性值
https://www.cnblogs.com/hafiz/p/5876243.html
五種方式讓你在java中讀取properties文件內容再也不是難題html

思路:建立工具類 實現讀取配置文件存入緩衝流,判斷靜態變量在緩衝流中是否存在, 並賦值的過程java

-具體實現:apache

  • 1.經過工具類獲取properties文件流 存入Properties對象中
  • 2.獲取全部的靜態變量名稱存入list
  • 3.遍歷Properties 判斷Property 是否存在於list中 存在則從新賦值

注意事項:工具

  • 1.若配置文件在jar中 ,引入jar包後,第三方jar包的根目錄和src是同一個目錄.按正常方式添加便可.請使用以下方式讀取文件. PropertiesUtil.class.getResourceAsStream("/"+propName+".properties");
  • 2.使用反射修改類中的靜態變量的值
    Field field = AlipayConfig.class.getDeclaredField(key); field.set(AlipayConfig.class, value);
package com.youboy.order.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.youboy.alipay.constant.AlipayConfig;

/** 
* @author wanpan 
* @version 建立時間:2018年4月10日 上午10:00:53 
* 類說明 
*/
public class PropertiesUtil {

    private final static Logger logger = Logger.getLogger(PropertiesUtil.class);
    private static Properties props;
/*  static {
        loadProps("zfb_dev");
    }*/

    synchronized private static void loadProps(String propName) {
        logger.info("開始加載properties文件內容.......");
        props = new Properties();
        InputStream in = null;
        try {
            /*
             * <!--第一種,經過類加載器進行獲取properties文件流-->
             * in = PropertyUtil.class.getClassLoader().getResourceAsStream(
             * "jdbc.properties");
             * <!--第二種,經過類進行獲取properties文件流-->
             */
            in = PropertiesUtil.class.getResourceAsStream("/"+propName+".properties");
            props.load(in);
        } catch (FileNotFoundException e) {
            logger.error(propName+".properties文件未找到",e);
        } catch (IOException e) {
            logger.error("出現IOException",e);
        } finally {
            try {
                if (null != in) {
                    in.close();
                }
            } catch (IOException e) {
                logger.error(".properties文件流關閉出現異常",e);
            }
        }
        logger.info("加載properties文件內容完成...........");
        logger.info("properties文件內容:" + props);
    }

    public static boolean loadPropertitesToClass(String properties,Class c) {
        boolean b = false;
        loadProps(properties);
        ArrayList<String> filedNames = getFiledName(c);
        Enumeration<?> enumeration = props.keys();
        while (enumeration.hasMoreElements()) {
            String key = enumeration.nextElement().toString();
            String value = props.getProperty(key);          
            if (filedNames.contains(key)) {
                try {
                    Field field = AlipayConfig.class.getDeclaredField(key);
                    field.set(AlipayConfig.class, value);
                    b = true;
                } catch (Exception e) {
                    logger.error(c.getName()+"靜態變量賦值出錯",e);
                } 
            }

        }
        return b;
        
    }

    public static String getProperty(String key,String pros) {
        if (null == props) {
            loadProps(pros);
        }
        return props.getProperty(key);
    }

    public static String getProperties(String key) {
        if (null == props) {
            return null;
        }
        return props.getProperty(key);
    }

    private static ArrayList<String> getFiledName(Class c) {    
        Field[] fields = c.getDeclaredFields();     
        ArrayList<String> fieldNames = new  ArrayList<String>();                
        for (int i = 0; i < fields.length; i++) {           
            fieldNames.add(fields[i].getName());
        }
        return fieldNames;
    }

}
相關文章
相關標籤/搜索