In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to configure yaml in Spring Boot". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to configure yaml in Spring Boot.
First, application.yaml can be written in four different locations in Spring Boot, which are as follows:
In the config directory under the root of the project
Under the project root directory
In the config directory under classpath
Under the classpath directory
The priority of the application.yaml files in the four locations is reduced in the order listed above. That is, if the same attribute appears in all four files, the higher priority shall prevail.
So does application.yaml have to be called application.yaml? Of course it's not necessary. Developers can define the yaml name themselves. If you define it yourself, you need to specify the name of the configuration file when the project starts, as shown below:
Of course, this is configured directly in IntelliJ IDEA. If the project has been packed into a jar package, the following parameters are added when the project starts:
Java-jar myproject.jar-- spring.config.name=app
After this configuration, when the project starts, it will look for a file named app.yaml in order according to the four locations mentioned above. Of course, these four locations are not immutable and can be defined by yourself. There are two ways, one is to use the spring.config.location attribute, and the other is to use the spring.config.additional-location attribute. In the first attribute, it means that you redefine the location of the configuration file. When the project starts, it will look for the configuration file according to the defined location. This definition will overwrite the default four locations. The second method can also be used, and the second method means that several locations are added on the basis of four locations, and the priority of the newly added location is higher than the original location.
The configuration is as follows:
Note here that the value must end with / when configuring the file location.
Array injection
Yaml also supports array injection, such as
My:
Servers:
-dev.example.com
-another.example.com
This data can be bound to an array with Bean:
@ ConfigurationProperties (prefix= "my")
@ Component
Public class Config {
Private List servers = new ArrayList ()
Public List getServers () {
Return this.servers
}
}
After the project starts, the array in the configuration is automatically stored in the servers collection. Of course, yaml can store not only this simple data, but also objects in collections. For example, the following:
Redis:
RedisConfigs:
-host: 192.168.66.128
Port: 6379
-host: 192.168.66.129
Port: 6380
This can be injected into the following classes:
@ Component
@ ConfigurationProperties (prefix = "redis")
Public class RedisCluster {
Private List redisConfigs
/ / omit getter/setter
} advantages and disadvantages
Unlike the disorder of properties files, yaml configuration is orderly, which is useful in some configurations, such as Spring Cloud Zuul configuration, where order is particularly important when we configure proxy rules. Of course, yaml configuration is not everything. For example, yaml configuration currently does not support @ PropertySource annotations.
Thank you for reading, the above is the content of "how to configure yaml in Spring Boot". After the study of this article, I believe you have a deeper understanding of how to configure yaml in Spring Boot, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.