Home > Java > javaTutorial > body text

How to specify the value of the configuration properties file in Springboot

WBOY
Release: 2023-06-02 20:52:06
forward
1657 people have browsed it

First create a configuration file test_config.properties:

How to specify the value of the configuration properties file in Springboot

test.number=123456789
Copy after login

Then get the value corresponding to test.number

Here we take the most direct method (It can also be obtained through annotations). We specially prepared a tool class 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"));
    }
}
Copy after login

OK. Test the main method of the tool class:

How to specify the value of the configuration properties file in Springboot

The above is the detailed content of How to specify the value of the configuration properties file in Springboot. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!