1. 程式人生 > >Java配置檔案讀取和路徑設定

Java配置檔案讀取和路徑設定

記錄幾種讀取配置檔案的方法,以及配置檔案的放置路徑。

1、使用PropertiesLoaderUtils工具類(springframework包提供)

優點:實時載入配置檔案,修改後立即生效,不必重啟

配置檔案至於classpath中(與class檔案放在一起,如果打jar包需打到包內),程式碼如下:

private static void springUtil(){  
    Properties props = new Properties();  
    while(true){  
        try {  
            props=PropertiesLoaderUtils.loadAllProperties("param.properties");  
            for(Object key:props.keySet()){  
                System.out.print(key+":");  
                System.out.println(props.get(key));  
            }  
        } catch (IOException e) {  
            System.out.println(e.getMessage());  
        }  
          
        try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}  
    }  
}  

2、根據檔案路徑讀取

優點:配置檔案可以放在jar包外面,根據檔案路徑尋找配置檔案

程式碼如下:

public static String readValue(String fileName, String key)
{
    Properties props = new Properties();
    String value = null;
    try
    {
        // 配置檔案位於當前目錄中的config目錄下
        InputStream in = new BufferedInputStream(new FileInputStream("config/" + fileName));
        props.load(in);
        value = props.getProperty(key);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return value;
}

3、Spring載入配置檔案的兩種方式

1)按classpath載入(配置檔案與class檔案放於同一目錄)

初始化Spring程式碼:

public static boolean initialize() {
        if (isInitialize) {
            return true;
        }

        try {
            appContenxt = new FileSystemXmlApplicationContext("spring.xml");
            isInitialize = true;
            return true;
        }
        catch (Exception e) {
            logger.error("Initialize spring framework failed.", e);
            return false;
        }
    }
配置檔案格式:
<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>jdbc.properties</value>
            <value>param.properties</value>
        </list>
    </property>
</bean>

ibatis配置寫法:

<sqlMapConfig>
    <settings cacheModelsEnabled="true" enhancementEnabled="true"
        lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="1024"
        maxSessions="1000" maxTransactions="16" useStatementNamespaces="true" />
    <sqlMap resource="sqlmap/sqlmap-global.xml" />
    <sqlMap resource="sqlmap/sqlmap-memo.xml" />
    <sqlMap resource="sqlmap/sqlmap-city.xml" />
</sqlMapConfig>

2)按檔案路徑載入(比如配置檔案位於當前目錄中的config目錄下)

初始化Spring程式碼:

public static boolean initialize() {
        if (isInitialize) {
            return true;
        }

        try {
            appContenxt = new FileSystemXmlApplicationContext("file:config/spring.xml");
            isInitialize = true;
            return true;
        }
        catch (Exception e) {
            logger.error("Initialize spring framework failed.", e);
            return false;
        }
    }
配置檔案格式:
<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>file:config/jdbc.properties</value>
            <value>file:config/param.properties</value>
        </list>
    </property>
</bean>
ibatis配置寫法:
<sqlMapConfig>
    <settings cacheModelsEnabled="true" enhancementEnabled="true"
        lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="1024"
        maxSessions="1000" maxTransactions="16" useStatementNamespaces="true" />
    <sqlMap url="file:config/sqlmap/sqlmap-global.xml" />
    <sqlMap url="file:config/sqlmap/sqlmap-windcustomcode.xml" />
    <sqlMap url="file:config/sqlmap/sqlmap-shiborprices.xml" />
</sqlMapConfig>

參考文章: