java中讀取properties文件的幾種方式

Java 開發中,需要將一些易變的配置參數放置 properties 配置文件中。我們在使用中需要 讀取參數使用,在properties 文件中存放也是key/value格式

image_host=http://192.168.22.122/

方式一:在spring的配置文件spring.xml中先找到資源文件

    <!-- 使用spring自帶的佔位符替換功能 -->
    <bean
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <!-- 	允許JVM參數覆蓋
            java -Djdbc.url=123 -jar xxx.jar -->
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
        <!-- 忽略沒有找到的資源文件 -->
        <property name="ignoreResourceNotFound" value="true"/>
        <!-- 	配置資源文件 -->
        <property name="locations">
            <list>
                <!--<value>classpath:disconf.properties</value>-->
            </list>
        </property>
    </bean>

在這裏需要注意的一點是,我們在項目啓動後,加載完這個xml後,需要添加掃描的包,這些包裏面的類才能通過@Value("$()")去獲取配置文件的值

    @Value("${image_host}")
    private String image_host;

方式二:使用託管方式的disconf配置(無代碼侵入, 配置更改會自動reload)

在pom加入

    <!-- disconf 2.6.31 -->
    <dependency>
      <groupId>com.baidu.disconf</groupId>
      <artifactId>disconf-client</artifactId>
      <version>2.6.31</version>
      <exclusions>
        <exclusion>
          <artifactId>javassist</artifactId>
          <groupId>org.javassist</groupId>
        </exclusion>
        <exclusion>
          <artifactId>com.101tec</artifactId>
          <groupId>zkclient</groupId>
        </exclusion>
        <exclusion>
          <groupId>com.google.guava</groupId>
          <artifactId>guava</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

在spring的配置文件中加入

    <bean id="configproperties_disconf" class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:/disconf.properties</value>
            </list>
        </property>
    </bean>

    <bean class="com.lf.utils.PropertyUtil">
        <property name="ignoreResourceNotFound" value="true"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="propertiesArray">
            <list>
                <ref bean="configproperties_disconf"/>
            </list>
        </property>
    </bean>

準備工具類

/**
 * @program: lfmyFirst
 * @description:獲取配置文件
 * @author: LingFeng
 * @create: 2019-09-29 11:01
 */
public class PropertyUtil extends ReloadingPropertyPlaceholderConfigurer {
    /**
     * 配置屬性對象
     */
    private static Properties properties;

    public synchronized static void newProperties() {
        properties = new Properties();
    }
   //初始化配置
    @Override
    public void setPropertiesArray(Properties[] propertiesArray) {
        super.setPropertiesArray(propertiesArray);
        newProperties();
        for (Properties propertiesTemp : propertiesArray) {
            properties.putAll(propertiesTemp);
        }
    }
    private static String getValueByKey(String propertiesKey, String defaultValue) {

        return properties.getProperty(propertiesKey, defaultValue);
    }

    public static String getString(String propertiesKey) {
        String value = "";
        try {
            value = new String(getValueByKey(propertiesKey, "").getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            logger.error("getString(" + propertiesKey + ")", e);
        }
        return value;
    }

}

讀取的話可以直接調用我們的工具類獲取

 PropertyUtil.getString("image_host")

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

這些代碼在我新的項目將使用,歡迎大家糾正改進

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章