Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the SpringBoot configuration file?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you how the SpringBoot configuration file is, concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Environment: springboot2.2.13

There are two configuration files in SpringBoot

Bootstrap (.yml or .properties)

Application (.yml or .properties)

Next, let's talk about the difference between the two profiles!

The difference between bootstrap/ application

Bootstrap.yml (bootstrap.properties) loads first

Load after application.yml (application.properties)

Bootstrap.yml is used in the boot phase of the application context and is loaded by the parent Spring ApplicationContext. The parent ApplicationContext is loaded before using application.yml.

There are two contexts in Spring Boot, one is bootstrap and the other is application. Bootstrap is the parent context of the application, that is, bootstrap loading takes precedence over applicaton. Bootstrap is mainly used to load configuration information from additional resources and to decrypt properties in local external configuration files. These two contexts share an environment that is the source of external properties of any Spring application. The properties in bootstrap are loaded first, and they cannot be overridden by the same local configuration by default, which means that the configuration in bootstrap cannot be overridden.

Application scenarios of bootstrap/ application

The application configuration file is easy to understand and is mainly used for automated configuration of Spring Boot projects.

The bootstrap profile has the following application scenarios.

When using the Spring Cloud Config configuration Center, you need to add the configuration properties connected to the configuration Center in the bootstrap configuration file to load the configuration information of the external configuration Center.

Some fixed attributes that cannot be overridden

Some encryption / decryption scenarios

The following is an official description of bootstrap. [yml/properties]:

A Spring Cloud application operates by creating a "bootstrap" context, which is a parent context for the main application. This context is responsible for loading configuration properties from the external sources and for decrypting properties in the local external configuration files. The two contexts share an Environment, which is the source of external properties for any Spring application. By default, bootstrap properties (not bootstrap.properties but properties that are loaded during the bootstrap phase) are added with high precedence, so they cannot be overridden by local configuration. The bootstrap context uses a different convention for locating external configuration than the main application context. Instead of application.yml (or .properties), you can use bootstrap.yml, keeping the external configuration for bootstrap and main context nicely separate custom configuration file

@ Configuration

@ ConfigurationProperties (prefix = "pack")

@ PropertySource (value = "classpath:config.properties")

Public class PackProperties {

Private String name

Private Integer age

}

Pack.name=xxxx pack.age=10

Note: @ PropertySource can only load properties files, not yml files.

Import the Spring XML file @ Configuration @ ImportResource (locations = {"classpath:application-jdbc.xml", "classpath:application-aop.xml"}) public class ResourceConfig {}

Both application-jdbc.xml,application-aop.xml configuration files must be configuration files for spring.

Profile default

The code is as follows:

@ Value ("${pack.name}") private String name

When pack.name is not configured in the configuration file, the service startup will report an error, and we can prevent errors by setting default values.

@ Value ("${pack.name:xx}") private String name

Here the colon ":" is followed by the default value of the setting, and you can write nothing here with ${pack.name:}.

Load the configuration file for the development environment

In general, we will set different configuration files for the test environment, development environment and production environment, and load different configuration files for different environments.

The following configuration files are available:

The build environment loads the prod file, the test environment loads the test file, and the development environment loads the dev file. Just add the following configuration to the application.yml configuration file.

Spring: profiles: active:-dev

This is written dead in the configuration file, or we can make it on the command line.

Java-jar xxxxx.jar-- spring.profiles.active=dev

With include containing multiple configuration files, include is environment-independent (that is, it will be loaded regardless of the environment)

Spring: profiles: active:-dev include:-cloud-secret configuration file loading order

View

ConfigFileApplicationListener.java source code, which defines where to load the configuration file.

Loading order:

File:./config/

File:./config/*/

File:./

Classpath:config/

Classpath:

Priority from high to low, high coverage low.

Application.yml or application.properties files are loaded by default.

You can specify the name of the configuration file when you start the application

Java-jar xxxxx.jar-- spring.config.name=myapp

Source code:

Read the configuration file @ SpringBootApplication public class SpringBootJwtApplication implements ApplicationRunner {public static void main (String [] args) {SpringApplication.run (SpringBootJwtApplication.class, args);} @ Override public void run (ApplicationArguments args) throws Exception {ClassPathResource resource = new ClassPathResource ("config.properties"); try {Properties properties = PropertiesLoaderUtils.loadProperties (resource) through code System.out.println (properties);} catch (IOException e) {e.printStackTrace ();}

Here, resources are loaded through the PropertiesLoaderUtils utility class

The above is what the SpringBoot configuration file looks like. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report