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 configuration file format for SpringBoot

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "what is the configuration file format of SpringBoot". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the configuration file format of SpringBoot"?

1 configuration file format for Spring Boot

Application.properties or application.yml, the main difference between them is the different writing format.

1). Properties:

Springboot.user.name = testname

2) yml:

Springboot: user: name: testname

In addition, the properties of the .properties format file are unordered, and the properties of the .yml format file are ordered, but it does not support the @ PropertySource annotation import configuration.

Core comments of 2 Spring Boot

The above annotation of the startup class is @ SpringBootApplication, which is also the core annotation of SpringBoot. The main combination includes the following three annotations:

@ SpringBootConfiguration: combines @ Configuration annotations to implement the function of configuration files.

EnableAutoConfiguration: turn on the auto-configuration feature, or turn off an auto-configuration option, such as turning off the data source auto-configuration feature: @ SpringBootApplication (exclude = {DataSourceAutoConfiguration.class}).

@ ComponentScan:Spring component scan.

3 how to enable the Spring Boot feature

1) inherit the spring-boot-starter-parent project

2) Import spring-boot-dependencies project dependencies

4 how to run Spring Boot

1) directly execute the main method to run

2) run with the Maven / Gradle plug-in

3) package it into a jar and run it directly through the java-jar command

5 how Spring Boot reads the configuration

Spring Boot can bind variables through @ PropertySource,@Value,@Environment,@ConfigurationProperties.

Monitor in 6 Spring Boot

Spring Boot actuator is one of the important functions in the Spring startup framework. The Spring Boot Monitor helps you access the current status of running applications in a production environment. For example, what bean was created, the mapping in the controller, CPU usage, and so on. You can then automatically collect and audit health status (health) and indicators (metric) into your application.

7 the principle of automatic configuration of Spring Boot

Add @ SpringBootApplication or @ EnableAutoConfiguration to the main method of the Spring program. Automatically go to maven to read the spring.factories file in each starter, which is configured with all the bean that needs to be created in the spring container.

Load AutoConfigure classes based on spring.factories configuration

Automatically configure and inject Bean into Spring Context according to the conditions of @ Conditional annotation

8spring-boot-maven-plugin

Spring-boot-maven-plugin provides commands to package or run applications like jar.

Run: run your Spring Boot application.

Repackage: repackage your jar package or war package to make it executable

Start and stop: manage the lifecycle of Spring Boot applications, or for integration testing.

9 use the configuration file to configure the configuration of a specific environment through Spring Boot

For example, there are now two configuration files, application-dev.properties and application-prod.properties. The configuration of the application-prod.properties configuration file can be loaded by using spring.profiles.active=prod in application.properties.

10 how to disable a specific autoconfiguration class

@ SpringBootApplication (exclude= {Order.class})

@ EnableAutoConfiguration (exclude= {DataSourceAutoConfiguration.class})

@ EnableAutoConfiguration (excludeName= {Foo.class})

Loading of 11Spring Boot factory mode

A factory loading mechanism (Factory Loading Mechanism) is used internally in Spring Framework. This mechanism is done using SpringFactoriesLoader, and SpringFactoriesLoader uses the loadFactories method to load and instantiate factories from the spring.factories files in the META-INF directory, which are found in the jar package in classpath.

At this point, I believe you have a deeper understanding of "what is the configuration file format of SpringBoot". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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.

Share To

Development

Wechat

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

12
Report