In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how Java uses ConfigurationProperties to obtain the configuration in yml". The editor shows you the operation process through an actual case. The method of operation is simple, fast and practical. I hope that this article "how Java uses ConfigurationProperties to get the configuration in yml" can help you solve the problem.
Use ConfigurationProperties to get the configuration of yml
In the process of development, we often encounter scenarios that require custom configuration, such as configuring an ip, an address, etc., and writing them into the yml file. In the project, we use @ Value ("${xxxx.xxxx}") to get the custom configuration. In fact, this is a bit cumbersome. When you define a configuration, you need to write a @ Value to get it, so why not use a java config to get the configuration?
Usage
Write yml configuration file
All three configurations of user: config: # user_name user-name userName can be identified by user_name: "zhangsan" age: "20" exmail: "123@123.com" address: "Mars"
Write Java config classes
/ / the get and set methods need to be overridden. Here the lombok annotation is used instead of @ Data// to instantiate @ Component// in the spring container to get the configuration information prefixed with user.config, and compare it with the attributes under this class to bind @ ConfigurationProperties (prefix = "user.config") public class UserConfig {private String userName; private String age; private String exmail; private String address;}.
Inject where you need it
@ Resource private UserConfig userConfig
test
@ ConfigurationProperties cannot get the profile attribute value problem
Application.yml
Configuration class
@ Component@ConfigurationProperties (prefix = "system") public class SystemConfig {/ * * Project name * / private static String name; / * * version * / private String version; / * * copyright year * / private String copyrightYear; / * instance demo switch * / private boolean demoEnabled / * windows environment, file upload path (local upload) * / private static String winUploadPath; / * other system environments (linux, Mac...) In the environment, the file upload path (local upload) * / private static String otherUploadPath; / * * gets the address switch * / private static boolean addressEnabled; public static String getName () {return name;} public void setName (String name) {SystemConfig.name = name;} public String getVersion () {return version } public void setVersion (String version) {this.version = version;} public String getCopyrightYear () {return copyrightYear;} public void setCopyrightYear (String copyrightYear) {this.copyrightYear = copyrightYear;} public boolean isDemoEnabled () {return demoEnabled;} public void setDemoEnabled (boolean demoEnabled) {this.demoEnabled = demoEnabled;} public static String getWinUploadPath () {return winUploadPath } public static void setWinUploadPath (String winUploadPath) {SystemConfig.winUploadPath = winUploadPath;} public static String getOtherUploadPath () {return otherUploadPath;} public static void setOtherUploadPath (String otherUploadPath) {SystemConfig.otherUploadPath = otherUploadPath;} public static boolean isAddressEnabled () {return addressEnabled;} public void setAddressEnabled (boolean addressEnabled) {SystemConfig.addressEnabled = addressEnabled } / * judge the current operating system and return the corresponding local upload path * * @ return String * @ author Liangyixiang * @ date 2021-11-15 * * / public static String getUploadPath () {OsInfo osInfo = SystemUtil.getOsInfo () / / determine the system environment to obtain the upload path if (ObjectUtils.isNotEmpty (osInfo) & & osInfo.isWindows ()) {return getWinUploadPath ();} else {return getOtherUploadPath ();} / * obtain the business system name * / public static String getSystemName () {return getName ();}}
Name, addressEnabled and winUploadPath, otherUploadPath are all static member variables, but their name and addressEnabled can get the value of the configuration file, but winUploadPath and otherUploadPath cannot.
The reason is that
The ser methods corresponding to winUploadPath and otherUploadPath are also defined as static methods.
This is the end of the introduction to "how Java uses ConfigurationProperties to get the configuration in yml". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.