In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "Spring Boot how to correctly read configuration file properties", so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Spring Boot how to correctly read configuration file properties" article.
@ Value
@ Value is used to read the value of the property in the application.yml configuration file.
Sample code
Attributes in the application.yml file:
/ / define attribute fileName: testisFile: falsefilePath: c://test
@ value reads the value of the application.yml attribute:
@ Configurationpublic class FileConfig {@ Value ("${fileName}") private final String fileName; @ Value ("${isFile}") private boolean isFile; @ Value ("${filePath}") private static String filePath;}
Test:
@ Autowired private FileConfig fileConfig; @ GetMapping ("getFileConfig") public void getFileConfig () {logger.info ("fileConfig: {}", fileConfig);}
Running result:
FileConfig:FileConfig [fileName=, isFile=false, filePath=null]
Pay special attention to:
@ Value cannot read the property value into a static variable, otherwise the read value will be empty.
@ Value cannot read the property value from a constant, otherwise the read value will be empty.
@ Value cannot read the value of type boolean. The tested version of Spring Boot2.1 is invalid. Version 2.2 or above supports it.
Therefore, it is personally recommended to minimize the use of @ Value annotations to read attribute values when it is not necessary.
@ ConfigurationProperties
Read profile values and convert them to class objects, making it easy to obtain values and modify property values.
Sample code
Attributes in the application.yml file:
Http: pool: # connection timeout connectTimeout: 5000 # get connection timeout connectionRequestTimeout: 1000 # number of connections per route defaultMaxPerRoute: 50 # / maximum number of connections in the connection pool maxTotal: 50 # time when the server returns data (response) socketTimeout: 5000 # defines the time of inactivity (in milliseconds), connection recycling validateAfterInactivity: 30000
@ ConfigurationProperties reads the attribute values that start with http.pool in application.yml:
/ / starting with http.pool @ Component@ConfigurationProperties (prefix = "http.pool") public class HttpClientConfig implements Serializable {private static final long serialVersionUID =-4608251658338406043L; / * * maximum number of connections * / private Integer maxTotal; / * * Route is a breakdown of the maximum number of connections * number of connections per route base * / private Integer defaultMaxPerRoute / * connection timeout * / private Integer connectTimeout; / * get the connection timeout from the connection pool * / private Integer connectionRequestTimeout; / * the time the server returns data (response) * / private Integer socketTimeout
Test:
GetMapping ("getHttpClientConfig") public void getHttpClientConfig () {String json=FastJsonUtil.toJSONString (httpClientConfig); logger.info ("fileConfig: {}", json);}
Property nesting:
@ ConfigurationProperties can nest List, map, class
Config: url: http://localhsot:8080 gaode-map: host: https://restapi.amap.com/v3 key: 1234@ConfigurationProperties (prefix= "config") public class Config {/ / Amap Information private GaodeMap gaodeMap;}
Pay special attention to:
Entities are not injected into spring containers by default and need to be used in conjunction with @ EnableConfigurationProperties or @ Component, otherwise the injected object is empty.
@ EnableConfigurationProperties
Inject the @ ConfigurationProperties read object into the spring container. For example, the above example can also use @ EnableConfigurationProperties to inject
@ EnableConfigurationProperties (HttpClientConfig.class) public class FileController {private Logger logger = LoggerFactory.getLogger (FileController.class); @ Autowired private FileConfig fileConfig; @ GetMapping ("getHttpClientConfig") public void getHttpClientConfig () {String json=FastJsonUtil.toJSONString (httpClientConfig); logger.info ("fileConfig: {}", json);}} @ ConfigurationPropertiesScan
Used to scan the @ ConfigurationProperties entity class and inject the class into the Spring container, the above example can be used as follows
@ ConfigurationPropertiesScan ("com.xx.fw.config") public class FwCoreApplication {public static void main (String [] args) {SpringApplication.run (FwCoreApplication.class, args);}} @ PropertySource
@ PropertySource is mainly used to read the specified configuration file, which needs to be used in conjunction with the @ ConfigurationProperties annotation to implement the configuration file and Java Bean injection operation.
Sample code
Properties file user.properteis:
User.id=222user.name= Kensei user.age=28
Entity class definition:
@ Component@ConfigurationProperties (prefix = "user") @ PropertySource (value = {"classpath:user.properties"}) public class UserConfig {private String id; private String name; private int age;}
Test:
GetMapping ("getUserConfig") public void getUserConfig () {String json=FastJsonUtil.toJSONString (userConfig); logger.info ("userConfig: {}", json);}
Output result:
C.s.fw.controller.FileController-userConfig: {"age": 28, "id": "123"," name ":" admin "} the above is about" how to correctly read the properties of the configuration file by Spring Boot ". I believe everyone has some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
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.