In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces SpringBoot how to achieve the replacement of configuration files, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Replacement of SpringBoot configuration files with spring.profiles.active
At work, the problem that must be encountered when testing or launching is to modify the configuration. Generally speaking, several configurations are available: dev (development environment), test (test environment), and pro (production | online environment). At this point, we need to comment out all the configurations in the configuration file, and then unlock the required configurations. At this point, this problem can be solved through the configuration of spring.profiles.active.
Application.properties
Create different properties files, such as application-dev.properties,application-test.properties,application-pro.properties, according to different environments.
Create an application.properties as follows:
Spring.profiles.active=dev # test testing, pro production
Then change the different configuration by modifying the application.properties.
Application.yml
As above, different yml profiles are created according to different environments, distinguished by suffixes (e.g. application-dev.yml).
Create an application.yml as follows:
Spring: profiles: active: test # dev development, pro production
As above, you only need to change the application.yml file to modify the configuration file.
SpringBoot read profile summary 1, use annotation @ ConfigurationProperties mapping
You can automatically map the configuration in the configuration file to the entity application.properties configuration by annotating @ ConfigurationProperties (the prefix of key in the prefix= configuration file).
2. Use annotated Value ("${key}") to map
You can map the values in the configuration file to a field of Bean managed by Spring through the @ Value annotation.
3. Use the annotation @ PropertySource ("classpath:xxx.properties")
Load comments for custom properties files
It is specially used to load the properties file at the specified location, which needs to be used in conjunction with @ ConfigurationProperties (prefix = ""). Spring does not provide an annotation for loading the yml file at the specified location.
4. Use Environment
This environment configuration class can read not only configuration files, but also system variables, such as jdk version, current operating system type, user directory, and so on.
5. Use @ ImportResource annotations
Use to load external configuration files into the program, for example, we define a beans.xml file with a bean configured in it, and the bean is not loaded into the Spring container by default. We need the @ ImportResource (locations = {"classpath:beans.xml"}) annotation to load this configuration file. SpringBoot recommends that you use configuration classes to add components to the container.
Thank you for reading this article carefully. I hope the article "how to replace configuration files in SpringBoot" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you 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.