In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the method of setting the path of the SpringBoot framework configuration file". In the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
SpringBoot profile path Settings
Select "Edit Configurations":
Modify the value of VM options in the startup configuration of springboot:
Value reference:
-Dspring.config.location=E:/workspace/xxxx/application.yml-Dspring.config.location=E:/workspace/xxxx/db.yml custom profile path and multi-profile profile 1. What is classpath
Classpath refers to the location where * .java files and resource files are compiled and stored. For Maven projects, it refers to the path of target/classes. As long as the compiled files are in this directory, springboot can find them. We refer to Maven projects here, and Java Web projects may be different.
Second, customize the path of springboot configuration file
The default location of springboot project configuration file application.properties/application.yml is classpath:/, classpath:/config/, file:./ and file:./config/. As long as our compiled file is located in these four locations, springboot can load the configuration file. But sometimes we need to configure multiple environment profiles with the environment name as the identity. As follows, we need to place the configuration file under the specified environment ID.
As shown in the figure above, the path of our resource file is no longer the default configuration path of springboot, so when springboot starts, we will not be able to load the configuration file, so we need to manually specify the loading path of the configuration file in the startup class.
@ SpringBootApplicationpublic class Springboot01Application {public static void main (String [] args) {/ / springboot default profile path / / String addClassPath = "spring.config.location:classpath:/"; / / Custom profile path String addClassPath = "spring.config.additional-location:classpath:/"; addClassPath + = ", classpath:/config/env/"; new SpringApplicationBuilder (Springboot01Application.class). Properties ("spring.config.name:application", addClassPath). Build (). Run (args);}} III. Switching of multiple profiles configuration files
When we usually develop a project, there will be different environment configuration files in different periods, such as application-dev.properties, application-sit.properties, application-uat.properties, application-pro.properties, so how can we switch the corresponding environment configuration files according to the current environment when we run the project?
Springboot provides us with a way to switch configuration files. When we write configuration files, the file name can be: application- {profile} .properties or application- {profile} .yml
1. Application- {profile} .properties mode
1. The configuration files of each environment are as follows:
Application-dev.properties
Person.name=xiaomaomao-devperson.pet=dog-devperson.hobby=LOL-devperson.cleverLevel=nine-dev
Application-sit.properties
Person.name=xiaomaomao-sitperson.pet=dog-sitperson.hobby=LOL-sitperson.cleverLevel=nine-sit
Application-uat.properties
Person.name=xiaomaomao-uatperson.pet=dog-uatperson.hobby=LOL-uatperson.cleverLevel=nine-uat
Application-pro.properties
Person.name=xiaomaomao-properson.pet=dog-properson.hobby=LOL-properson.cleverLevel=nine-pro
Select the profile that needs to be activated through application.properties
Spring.profiles.active=sit
II. Entity class
@ Configuration@PropertySource ("classpath:config/env/application.properties") @ ConfigurationProperties (prefix = "person") / / omitted get/setpublic class Person {private String name; private String pet; private String hobby; private String cleverLevel;}
III. Controller
@ RestControllerpublic class SpringbootProfileController {@ Autowired private ApplicationContext ioc; @ RequestMapping (value = "/ profile") public String testProfiles () {Person person = ioc.getBean ("person", Person.class); System.out.println (person); return "hello profiles created!";}}
4. Test results
2. Application.yaml mode
Spring: profiles: active: uat # specify which profile to activate-server: port: 8082spring: profiles: dev-server: port: 8083spring: profiles: sit-server: port: 8084spring: profiles: uat "what is the method of setting the path of the SpringBoot frame profile?" that's it. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.