In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "Springboot how to specify the value of the configuration properties file", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how Springboot specifies how to get the value of the configuration properties file.
First create a configuration file test_config.properties:
Test.number=123456789 then gets the value corresponding to test.number.
Here we take the most direct approach (which can also be obtained through annotations) and specially prepare a utility 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; / * configuration file read * * @ Author: JCccc * @ Des: ElegantDay * / public class PropertiesUtil {private static Logger log = LoggerFactory.getLogger (PropertiesUtil.class); private static Properties props / / read in the project root folder / / static {/ / if (props = = null) {/ / props = new Properties (); / / try {/ / props.load (new FileInputStream ("/ testDemo/config/test_config.properties")) / /} catch (IOException e) {/ / log.error ("profile read exception", e); / /} / / resource folder read 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 ("profile read exception", e) }} / * obtain 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 () according to the key in the configuration file } / * obtain value according to the key in the configuration file (default value is given when the value is not obtained) * @ 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") in the configuration file); System.out.println ("No key&value in the configuration file, give the default value" + PropertiesUtil.getProperty ("test.numberNone", "default JCccc");}} OK, test the main method of the tool class:
At this point, I believe you have a deeper understanding of "how Springboot specifies how to get the value of the configuration properties file". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.