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

Principle Analysis of Springboot-yaml configuration and automatic configuration

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

Share

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

This article mainly explains "the principle analysis of Springboot-yaml configuration and automatic configuration". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "the principle analysis of Springboot-yaml configuration and automatic configuration".

Version arbitration center

Spring dependencies helps us rely on many commonly used jar packages. You don't need a version number to import these jar packages.

Such as:

Principle of org.apache.commons commons-lang3 automatic configuration

Profile configuration debug: true can print an automatic configuration report on the console. You can print all startup autoconfiguration and non-startup autoconfiguration classes.

@ SpringBootApplication

Marked on a class, indicating that this class is the main startup class of springboot.

@ Target ({ElementType.TYPE}) @ Retention (RetentionPolicy.RUNTIME) @ Documented@Inherited@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan (excludeFilters = {@ Filter (type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class}), @ Filter (type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class})) public @ interface SpringBootApplication {

@ EnableAutoConfiguration: automatic configuration is enabled, so we don't have to do a lot of configuration manually

@ AutoConfigurationPackage@Import ({AutoConfigurationImportSelector.class}) public @ interface EnableAutoConfiguration {

@ AutoConfigurationPackage

All components under the package where the main configuration class is located will be scanned into the spring container.

@ Target ({ElementType.TYPE}) @ Retention (RetentionPolicy.RUNTIME) @ Documented@Inherited@Import ({Registrar.class}) public @ interface AutoConfigurationPackage {

AutoConfigurationImportSelector

Import a component into the container through @ import: this component loads all automatic configuration classes, such as mysql, web, and so on

Eventually you will go to the META-INF/spring.factories location to find all the automatic configuration classes and load them into the container. These auto-configuration classes get rid of a lot of configurations we used to do with spring.

Yaml syntax

Literal quantity

Strings do not need quotation marks by default, single quotation marks and double quotation marks have a special purpose

Single quotation marks are specially escaped, such as\ noutput or\ n

Double quotation mark special characters are not escaped, such as\ nThe output is a space.

No addition is the same as adding single quotation marks, it will escape.

Loose binding

The property is written like a hump like an underscore-or an underscore, and the conversion to an entity class is hump. But this can only be used in configurationProperties, not in @ Value annotations

Org.springframework.boot spring-boot-configuration-processor true

With this note, you can prompt the custom configuration in the yaml configuration.

Used with @ PropertySource annotations

The @ PropertySource annotation can load other files you specify

@ PropertySource (value = "classpath:user.properties") is used with @ ImportResource

Import the configuration file of spring to make it effective

@ ImportResource (locations= {"classpath:mybatis.xml"}) profile placeholder

${random.int} use the random number provided by yaml

${server.port} uses the previously configured value

${server.name: Hello} use the default value if there is no value

Profile

Activate to specify a different configuration environment

Command line activation can add-spring.profiles.active=dev

Virtual machine parameter activation-Dspring.profiles.active=dev

The loading order of the configuration file

File:. / config directory under the root path of the config/ project

File:. / project root directory

Classpath: config/

Classpath: /

All files will be loaded, from top to bottom priority from high to low, high will overwrite the low content. Different configurations will take effect and complement each other.

You can also use-spring.config.location to change the location of the configuration file when deploying the project. The configuration files loaded in the project complement those specified here.

At this point, I believe you have a deeper understanding of the "principle analysis of Springboot-yaml configuration and automatic configuration". 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