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

How does Spring Boot configure properties format and yml format of file types

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

Share

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

This article introduces the knowledge of "how to configure file types of properties format and yml format in Spring Boot". In the operation of actual cases, 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!

A profile type

Spring Boot supports configuration files in both properties and yml formats by default. Yml format is a natural tree interface, which is more clear at a glance than properties,yml. This is also our recommended configuration file format.

Examples of properties format:

Server.port=8090server.session-timeout=30server.tomcat.max-threads=0server.tomcat.uri-encoding=UTF-8spring.datasource.url=jdbc:mysql://localhost:3306/newbirdsspring.datasource.username=rootspring.datasource.password=mymysqlspring.datasource.driverClassName=com.mysql.jdbc.Driverspring.jpa.database=MYSQLspring.jpa.show-sql=truespring.jpa.hibernate.ddl-auto=updatespring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategyspring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

Examples of yml format:

Server: port: 8090 session-timeout: 30 tomcat.max-threads: 0 tomcat.uri-encoding: UTF-8spring: datasource: url: jdbc:mysql://localhost:3306/newbirds username: root password: mymysql driverClassName: com.mysql.jdbc.Driver jpa: database: MYSQL show-sql: true hibernate: ddl-auto: update naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy properties: hibernate: dialect: org.hibernate.dialect. MySQL5Dialect II profile 2.1Core profile

Spring Boot supports two core profiles by default: bootstrap.yml (bootstrap.properties) and application.yml (application.properties). Bootsrap loads first.

2.1.1 bootstrap.yml (bootstrap.properties)

Bootstrap.yml is used for the boot node above of the application and is loaded by the parent Spring ApplicationContext. We can simply understand the configuration information in bootstrap as system-level parameters. These parameters will not be changed easily.

The bootstrap configuration file has the following application scenarios:

When using Spring Cloud Config, you need to write spring.application.name and spring.cloud.config.server.git.uri to bootstrap.yml.

Some fixed attributes that cannot be overridden.

Some encryption / decryption scenarios. Encryption/decryption (encryption / decryption) is stored in bootstrap.yml.

Once the bootStrap.yml is loaded, the content will not be overwritten, and even if the content tag of the later loaded application.yml matches the tag of the bootstrap, the application will not overwrite the bootstrap.

2.1.2 application.yml (application.properties)

Application.yml is one of the most frequently used configuration files. Generally configure the configuration at the application level, such as the connection configuration of the database, if we use redis, we may write some redis-related configuration and so on.

2.2 Custom Profil

In order not to destroy the original ecology of the core file, but also need to have custom configuration information, we usually choose a custom configuration file to put these custom information, for example, we create a product.properties file in the resource directory.

The resources/product.properties file is as follows:

Ppid = 1000mmid = 1ccid = 10 three configuration files read 3.1 read core profile information

The core configuration file information value is read from bootstrap.yml (bootstrap.properties) and application.yml (application.properties) files.

3.1.1 Environment mode

This is done by relying on injection Evnironment. Add @ Autowired annotation to the created member variable private Environment env to complete dependency injection, and then use env.getProperty ("key name") to read the corresponding value. For example, the following code.

Add our custom attributes to the application.yml file:

# some custom attributes user: info: name: tuacy age: 27

Read configuration information by Environment:

@ RunWith (SpringRunner.class) @ SpringBootTest () public class PropertiesTest {private Environment environment; @ Autowired public void setEnvironment (Environment environment) {this.environment = environment;} @ Test public void test () {String name = environment.getProperty ("user.info.name"); Integer age = environment.getProperty ("user.info.age", Integer.class); System.out.println ("name =" + name + "; age =" + age) } 3.1.2 @ Value mode

You can get the corresponding value by including the key name in the core configuration file in @ Value's ${}.

@ RunWith (SpringRunner.class) @ SpringBootTest () public class PropertiesTest {@ Value ("${user.info.name}") private String name; @ Value ("${user.info.age}") private int age; @ Test public void test () {System.out.println ("name =" + name + "; age =" + age);} 3.1.3 @ ConfigurationProperties

When using @ ConfigurationProperties, be sure to add @ Configuration to the corresponding property class or specify a custom property class through the @ EnableConfigurationProperties annotation in any configuration class (recommended startup class). It is recommended to add @ Configuration annotation directly to the property class.

@ Configuration@ConfigurationProperties (prefix = "user.info") public class UserInfo {private int age; private String name; public String getName () {return name;} public void setName (String name) {this.name = name;} public int getAge () {return age;} public void setAge (int age) {this.age = age Read Custom profile Information 3.2.1 @ PropertySource + @ Value

@ PropertySource + @ Value is used to read the information from the property root configuration file. @ PropertySource specifies the configuration file path and encoding format, and @ Value specifies the key value in the configuration file.

Custom profile product.properties:

Ppid = 1000mmid = 1ccid = 10

The corresponding reading method:

@ Configuration@PropertySource (value = "classpath:product.properties", encoding = "utf-8") public class ProductInfo {@ Value ("${ppid}") private int pid; @ Value ("${mmid}") private int mid; @ Value ("${ccid}") private int cid; public int getPid () {return pid;} public void setPid (int pid) {this.pid = pid } public int getMid () {return mid;} public void setMid (int mid) {this.mid = mid;} public int getCid () {return cid;} public void setCid (int cid) {this.cid = cid;}} 3.2.2 @ ConfigurationProperties + @ PropertySource + @ Value

@ ConfigurationProperties + @ PropertySource + @ Value is suitable for reading configuration file information in yml format. @ PropertySource specifies the configuration file path and encoding format, and @ ConfigurationProperties and @ Value specify the key value in the configuration file.

Configuration file:

Admin: user: name: tuacy age: 25

How to read the corresponding configuration file:

@ Configuration@PropertySource (value = "classpath:role.yml") @ ConfigurationProperties (prefix = "admin.user") public class RoleUserInfo {@ Value ("${name}") private String name; @ Value ("${age}") private int age; public String getName () {return name;} public void setName (String name) {this.name = name;} public int getAge () {return age } public void setAge (int age) {this.age = age;}} loading order of four configuration files

The priority is from high to bottom, and the configuration with high priority will override the configuration with low priority.

Load all the configuration files and complement the configuration.

4.1 loading sequence of internal configuration files in the project

SpringBoot configuration files can be placed under multiple paths, and the configuration priority varies from path to path. Directories can be placed (priority from high to low)

Under the config directory under the root of the current project.

Under the current project root directory.

Under the config directory under the resources directory.

Under resource directory.

4.2 loading order of external configuration files for the project

Priority from high to low:

Command line argument

JNDI property from java:comp/env

Java system Properties (System.getProperties ())

Operating system environment variable

The random.* attribute value of the RandomValuePropertySource configuration

Application- {profile} .properties or application.yml (with spring.profile) configuration files outside the jar package

Application- {profile} .properties or application.yml (with spring.profile) configuration files inside the jar package

Application.properties or application.yml (without spring.profile) configuration files outside the jar package

Application.properties or application.yml (without spring.profile) configuration files inside the jar package

@ PropertySource on @ Configuration annotation class

Default properties specified by SpringApplication.setDefaultProperties

This is the end of the content of "how Spring Boot configures properties format and yml format of file types". 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: 215

*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