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


# 使用注解或其他靜態注入的方法 這里不討論

相關文章: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文件內容不再是難題

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

-具體實現:

  • 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;
	}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM