2024.4.22 Monday
Contents
- 3. YAML Configuration Injection
- 3.1. Learning YAML Syntax
- 3.2. Off-topic: What Exactly Do the Various Names Correspond to When Creating a New Project
- 3.3. Injecting Configuration Files
3. YAML Configuration Injection
3.1. Learning YAML Syntax
3.1.1. Creating a Project
As usual, modify the Maven, JDK, and Java versions in the settings and the JDK and Java versions in the Project Structure. Reload Maven and run again.
Solution to the problem of the new project not being correctly recognized in the original file:
https://www.jianshu.com/p/24919cc7f5b1
3.1.2. application.properties
spring.application.name=springboot-02-config
# What exactly can be configured in this springboot configuration file?
# 1. Official documentation URL: https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/htmlsingle/#boot-features-external-config
# You can see all the configuration statements at about the last quarter of the above webpage
# 2. Connection Principle
# However, using properties files is not recommended by the officials
3.1.3. Creating application.yaml
Can be found from pom.xml -> spring-boot-starter-parent (line7) -> as follows
3.1.4. Configuration Files
3.1.4.1. SpringBoot uses a global configuration file, the name of which is fixed.
3.1.4.2. application.properties
Syntax structure: key=value
3.1.4.3. application.yml
Syntax structure: key: space value
3.1.4.4. Purpose of the configuration files: To modify the default values of SpringBoot’s auto-configuration, as SpringBoot has them auto-configured at the lower levels;
3.1.5. Overview of YAML
3.1.5.1. YAML stands for “YAML Ain’t a Markup Language”, a recursive acronym. At the time of its development, YAML was initially meant as “Yet Another Markup Language”
3.1.5.2. This language is centered around data, not markup!
3.1.5.3. Previously, configuration files mostly used XML; let’s compare a simple port configuration in YAML and XML:
3.1.5.4. Traditional XML configuration:
<server>
<port>8081</port>
</server>
3.1.5.5. YAML configuration:
server:
port: 8080
3.1.6. Basic Syntax of YAML
3.1.6.1. Note: Syntax is strict!
3.1.6.1.1. Spaces must not be omitted.
3.1.6.1.2. Indentation controls the hierarchy, all data aligned on the left are at the same level.
3.1.6.1.3. Both keys and values are case sensitive.
3.1.6.2. Literals: Ordinary values [numbers, boolean values, strings]
3.1.6.2.1. Literals are written directly afterwards, strings do not need to be enclosed in double or single quotes by default
key: value
3.1.6.2.2. Notes
- " " Double quotes do not escape special characters inside the string; they are taken literally. For example: name: “zhang \n san” -> Output: zhang newline san
- ’ ’ Single quotes escape special characters, which will be output as normal characters. For example: name: ‘zhang \n san’ -> Output: zhang \n san
3.1.6.3. Objects/Map (Key-Value Pairs)
3.1.6.3.1. Syntax
key:
value1:
value2:
key: {value1: xxx, value2: xxx,...}
3.1.6.3.2. Relationship between properties and values of an object
student:
name: zhangsan
age: 10
3.1.6.3.3. Inline syntax
student: {name: zhangsan, age: 0}
3.1.6.4. Arrays (List, Set)
- Use - value to denote an element in the array, for example:
pets:
- cat
- dog
- pig
- Inline syntax:
pets: [cat, dog, pig]
3.1.6.5. Changing the Default Port Number of SpringBoot
Add the parameter for the port number in the configuration file to switch ports
server:
port: 8082
3.1.6.6. Comparison
3.2. Off-topic: What Exactly Do the Various Names Correspond to When Creating a New Project
Remember to change the encoding set (when writing in Chinese in the properties configuration file, there will be garbled text, we need to set the encoding format to UTF-8 in IDEA;)
3.3. Injecting Configuration Files
YAML can directly inject matching values into entity classes.
Delete application.properties and clear the content of application.yaml file.
# Can be injected into the configuration class
# Literals: Ordinary values [numbers, boolean values, strings]
key: value
# Objects, Map (Key-Value Pairs)
# Objects, Map format
key:
value1:
value2:
key: {value1: xxx, value2: xxx,...}
student:
name: zhangsan
age: 10
student: {name: zhangsan, age: 0}
# Arrays (List, Set)
pets:
- cat
- dog
- pig
pets: [cat, dog, pig]
# Modifying the default port number of SpringBoot
server:
port: 8082
3.3.1. Create Dog.java
package com.p8.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component // Add to Spring's components
public class Dog {
@Value("wangcai") // Assign value
private String name;
@Value("3")
private Integer age;
// Shortcut key Alt+Insert
public Dog(){
}
public Dog(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
3.3.2. Create Person.java
package com.p8.pojo;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
public class Person {
private String name;
private Integer age;
private Boolean happy;
private Date birth;
private Map<String, Object> maps;
private List<Object> lists;
private Dog dog;
public Person(){
}
public Person(String name, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
this.name = name;
this.age = age;
this.happy = happy;
this.birth = birth;
this.maps = maps;
this.lists = lists;
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getHappy() {
return happy;
}
public void setHappy(Boolean happy) {
this.happy = happy;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", happy=" + happy +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
3.3.3. Enter Springboot02ConfigApplicationTests.java for testing
package com.p8.springboot02config;
import com.p8.springboot02config.pojo.Dog;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot02ConfigApplicationTests {
@Autowired
private Dog dog;
@Test
void contextLoads() {
System.out.println(dog);
}
}
(Click the run button on the left side of the file)