成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

.properties 配置文件讀取

ASCH / 869人閱讀

摘要:其中,因?yàn)槲覀儙缀跛械捻?xiàng)目都會(huì)使用,所以情境二在我個(gè)人的工作中使用最為頻繁。

.properties 配置文件讀取 情境一:JAVA 讀取 .properties 配置
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 屬性文件工具類
 */
public final class PropsUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(PropsUtil.class);
    
    /**
     * 加載屬性文件
     */
    public static Properties loadProps(String fileName) {
        Properties props = null;
        InputStream is = null;
        try {
            is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            if (is == null) {
                throw new FileNotFoundException(fileName + " file is not found");
            }
            props = new Properties();
            props.load(is);
        } catch (IOException e) {
            LOGGER.error("load properties file failure", e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    LOGGER.error("close input stream failure", e);
                }
            }
        }
        return props;
    }

    /**
     * 獲取字符型屬性(默認(rèn)值為空字符串)
     */
    public static String getString(Properties props, String key) {
        return getString(props, key, "");
    }

    /**
     * 獲取字符型屬性(可指定默認(rèn)值)
     */
    public static String getString(Properties props, String key, String defaultValue) {
        String value = defaultValue;
        if (props.containsKey(key)) {
            value = props.getProperty(key);
        }
        return value;
    }

    /**
     * 獲取數(shù)值型屬性(默認(rèn)值為 0)
     */
    public static int getInt(Properties props, String key) {
        return getInt(props, key, 0);
    }

    /**
     * 獲取數(shù)值型屬性(可指定默認(rèn)值)
     */
    public static int getInt(Properties props, String key, int defaultValue) {
        int value = defaultValue;
        if (props.containsKey(key)) {
            value = CastUtil.castInt(props.getProperty(key));
        }
        return value;
    }

    /**
     * 獲取布爾型屬性(默認(rèn)值為 false)
     */
    public static boolean getBoolean(Properties props, String key) {
        return getBoolean(props, key, false);
    }

    /**
     * 獲取布爾型屬性(可指定默認(rèn)值)
     */
    public static boolean getBoolean(Properties props, String key, boolean defaultValue) {
        boolean value = defaultValue;
        if (props.containsKey(key)) {
            value = CastUtil.castBoolean(props.getProperty(key));
        }
        return value;
    }
}
情境二:在 Spring MVC 配置文件 dispatcher-servlet.xml 文件中讀取 .properties 配置文件




    
    
情境三:使用 Spring MVC 注解@Value在項(xiàng)目啟動(dòng)時(shí)自動(dòng)注入屬性值 情境四:在 JSP 文件中通過自定義 tld 讀取配置文件屬性值

后記

在實(shí)際工作中,.properties 配置文件的使用情境,我個(gè)人就只能想到上面4種情境。其中,因?yàn)槲覀儙缀跛械捻?xiàng)目都會(huì)使用 Spring MVC,所以情境二在我個(gè)人的工作中使用最為頻繁。

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/66828.html

相關(guān)文章

  • ServletConfig與ServletContext對象詳解

    摘要:一對象在的配置文件中,可以使用一個(gè)或多個(gè)標(biāo)簽為配置一些初始化參數(shù)。進(jìn)而,程序員通過對象就可以得到當(dāng)前的初始化參數(shù)信息。對象通常也被稱之為域?qū)ο蟆? 一、ServletConfig對象 在Servlet的配置文件中,可以使用一個(gè)或多個(gè)標(biāo)簽為servlet配置一些初始化參數(shù)。(配置在某個(gè)servlet標(biāo)簽或者整個(gè)web-app下) 當(dāng)servlet配置了初始化參數(shù)后,web容器在創(chuàng)建se...

    X1nFLY 評論0 收藏0
  • SpringBoot 實(shí)戰(zhàn) (三) | 配置文件詳解

    摘要:前言如題,今天解析下的配置文件。這時(shí)我們就可以用的屬性來配置隨機(jī)數(shù),比如隨機(jī)字符串隨機(jī)隨機(jī)以內(nèi)的隨機(jī)數(shù)的隨機(jī)數(shù)使用多配置文件很多時(shí)候我們開發(fā)項(xiàng)目都需要很多套環(huán)境,比如有測試環(huán)境,開發(fā)環(huán)境以及生產(chǎn)環(huán)境。原因是,是以的編碼方式讀取配置文件。 微信公眾號:一個(gè)優(yōu)秀的廢人如有問題或建議,請后臺(tái)留言,我會(huì)盡力解決你的問題。 前言 如題,今天解析下 SpringBoot 的配置文件。 自定義屬性加...

    chenjiang3 評論0 收藏0
  • 「造個(gè)輪子」——cicada 設(shè)計(jì)一個(gè)配置模塊

    摘要:同時(shí)也新增了一個(gè)。將不同的配置文件用不同的對象進(jìn)行管理。由于需要支持多個(gè)配置文件,所有需要定義一個(gè)抽象類供所有的配置管理實(shí)現(xiàn)。其實(shí)就是一個(gè)結(jié)構(gòu)的緩存,用于存放所有的配置??偨Y(jié)這就是本次中的升級內(nèi)容,包含了配置支持以及代碼重構(gòu)。 showImg(https://segmentfault.com/img/remote/1460000016392132?w=2048&h=1365); 前言 ...

    fsmStudy 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<