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 syntax of the two configuration files in Springboot

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail about the syntax of the two configuration files in Springboot. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

SpringBoot uses a global configuration file whose name is fixed and has the following two types of files:

Application.propertiesapplication.ymlapplication.properties profile # literal (number, string, Boolean)

User.user-name= Zhang San

# object

User.user-name=45678

User.pwd=123456

User.mobile=18393948482

User.age=25

# Array

User.hobby= eats, sleeps and beats beans

# Map object

User.map.k1=123456

User.map..k2=456789

It is important to note that the string does not need to use double quotation marks or single quotation marks, if there is a write will only be regarded as part of the string, output as is.

The test code is as follows:

/ / read the configuration file and inject properties

Import lombok.Data

Import org.springframework.boot.context.properties.ConfigurationProperties

Import org.springframework.stereotype.Component

Import java.util.Map

@ Data

@ Component

@ ConfigurationProperties (prefix = "user")

Public class User {

Private String userName

Private String mobile

Private String pwd

Private int age

Private String [] hobby

Private Map map

}

/ / Test

Import com.example.demo.entity.User

Import org.junit.Test

Import org.junit.runner.RunWith

Import org.springframework.beans.factory.annotation.Autowired

Import org.springframework.boot.test.context.SpringBootTest

Import org.springframework.test.context.junit4.SpringRunner

@ RunWith (SpringRunner.class)

@ SpringBootTest

Public class DemoApplicationTests {

@ Autowired

User user

@ Test

Public void contextLoads () {

System.out.println (user)

String [] hobbys = user.getHobby ()

For (String str:hobbys

) {

System.out.println (str)

}

}

}

/ / output the result

2019-07-03 16 com.example.demo.DemoApplicationTests 56 com.example.demo.DemoApplicationTests 39.609 INFO 5596-[main] com.example.demo.DemoApplicationTests: Started DemoApplicationTests in 3.887 seconds (JVM running for 4.7)

User (userName=' Zhang San / nasty, mobile=18393948482, pwd=123456, age=25, hobby= [eat, sleep, beat beans], map= {K1133456, k214456789})

2019-07-03 16 o.s.s.concurrent.ThreadPoolTaskExecutor 56 INFO 39.851 5596-[Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor: Shutting down ExecutorService 'applicationTaskExecutor'

Application.yml configuration file yaml syntax introduction k: (space) v: represents a pair of key-value pairs (spaces must have); controls the hierarchical relationship with the indentation of spaces; as long as a column of data that is left-aligned is at the same level; attributes and values are also case-sensitive; K v: literally written; strings are not required to put single or double quotes by default; ": double quotes; special characters in the string will not be escaped The special character will be used as the meaning it wants to express: name: "zhangsan\ nlisi": output; zhangsan line feed lisi'': single quotation marks; escape the special character, which is ultimately just an ordinary string data -: represents a block, dividing a ymal file into multiple files yaml basic usage # literal quantity (number, string, Boolean)

Server:

Port: 8081

User:

User-name: Zhang San

Age: 26

Mobile: 18369615874

# Array inline writing

# hobby: [eating, sleeping, playing beans]

# map inline writing

# map: {K1: eat, K2: sleep}

Pwd: 123456

Hobby:

-eat.

-Sleep.

-beat Doudou

Map:

K1: eat

K2: sleep

-

# object inline writing

User: {user-name: Zhang San, pwd: 123456 mobile: 1839394721 Hobby: [eat, sleep, hit beans], map: {K1: eat, K2: sleep}, age: 26}

Verification results:

2019-07-03 17 com.example.demo.DemoApplicationTests 21V 32.074 INFO 32672-[main] com.example.demo.DemoApplicationTests: Started DemoApplicationTests in 3.201 seconds (JVM running for 3.923)

User (userName= Zhang San, mobile=18369615874, pwd=123456, age=26, hobby= [eat, sleep, hit beans], map= {K1 = eat, K2 = sleep})

Eat

sleep

Play Doudou on "how is the syntax of the two configuration files in Springboot" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

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

Internet Technology

Wechat

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

12
Report