bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

Springboot怎么指定獲取配置properties文件的值

本篇內容主要講解“Springboot怎么指定獲取配置properties文件的值”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Springboot怎么指定獲取配置properties文件的值”吧!

創新互聯為企業級客戶提高一站式互聯網+設計服務,主要包括成都做網站、成都網站制作、成都外貿網站建設重慶APP軟件開發、小程序開發、宣傳片制作、LOGO設計等,幫助客戶快速提升營銷能力和企業形象,創新互聯各部門都有經驗豐富的經驗,可以確保每一個作品的質量和創作周期,同時每年都有很多新員工加入,為我們帶來大量新的創意。 

先創建一個 配置文件test_config.properties:

Springboot怎么指定獲取配置properties文件的值

test.number=123456789

接下來獲取test.number對應的值

這里我們采取最直接的方式(也可以通過注解獲取),特意準備了個工具類 PropertiesUtil.java :

package com.test.webflux.util; 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils; 
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
 
/**
 * 配置文件讀取
 *
 * @Author: JCccc
 * @Des: ElegantDay
 */
public class PropertiesUtil { 
    private static Logger log = LoggerFactory.getLogger(PropertiesUtil.class);
    private static Properties props;
//項目根目錄文件夾內讀取
        // static {
        //     if (props == null) {
        //         props = new Properties();
        //         try {
        //             props.load(new FileInputStream("/testDemo/config/test_config.properties"));
        //         } catch (IOException e) {
        //             log.error("配置文件讀取異常", e);
        //         }
        //     }
        // }
 
//resource文件夾內讀取
       static {
           String fileName = "test_config.properties";
           props = new Properties();
           try {
               props.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName), "UTF-8"));
           } catch (IOException e) {
               log.error("配置文件讀取異常", e);
           }
       }
    /**
     * 根據配置文件中的key獲取value
     * @param key
     * @return
     */
    public static String getProperty(String key) {
        String value = props.getProperty(key.trim());
        if (StringUtils.isEmpty(value)) {
            return null;
        }
        return value.trim();
    }
    /**
     * 根據配置文件中的key獲取value (當獲取不到值賦予默認值)
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getProperty(String key, String defaultValue) {
        String value = props.getProperty(key.trim());
        if (StringUtils.isEmpty(value)) {
            value = defaultValue;
        }
        return value.trim();
    }
    public static void main(String[] args) {
        System.out.println("配置文件中有key&value:"+PropertiesUtil.getProperty("test.number"));
        System.out.println("配置文件無有key&value,賦予默認值"+PropertiesUtil.getProperty("test.numberNone","默認值 JCccc"));
    }
}

OK,測試下工具類的main方法:

Springboot怎么指定獲取配置properties文件的值

到此,相信大家對“Springboot怎么指定獲取配置properties文件的值”有了更深的了解,不妨來實際操作一番吧!這里是創新互聯網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

當前標題:Springboot怎么指定獲取配置properties文件的值
轉載注明:http://vcdvsql.cn/article12/gghhgc.html

成都網站建設公司_創新互聯,為您提供商城網站外貿建站面包屑導航電子商務企業網站制作微信公眾號

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

網站優化排名