YAML Configuration
In addition to the content summarized in Part 1, YAML has other syntax. For example, you can continue parameter references in YAML, as shown in the following example:
name: Bob
person:
name: ${name}
age: 20
@Value("${person.name}")
private String name;
@Value("${person.age}")
private int age;
@Test
void test2(){
System.out.println(name + ":" + age);
}
After running, the output result is Bob:20
.
This shows that in YAML, you can continue parameter references. person.name
is assigned the value of name
, which is Bob
.
Additionally, arrays in YAML can also be written in inline array format, for example:
list: [hello,world]
YAML also has an important knowledge point: scalars. For example, if we now define the following parameters in YAML:
msg1: 'hello\n world' #Does not recognize escape characters, outputs as-is
msg2: "hello\n world" #Recognizes escape characters
The only difference between the above parameters is that msg1
uses single quotes, while msg2
uses double quotes. We write the following code for testing:
@Value("${msg1}")
private String msg1;
@Value("${msg2}")
private String msg2;
@Test
void test3(){
System.out.println(msg1);
System.out.println(msg2);
}
The output result is as follows:
hello\n world
hello
world
This shows that if we use single quotes to quote strings, escape characters are not recognized and output as-is; if we use double quotes, escape characters are recognized.
So think about it, if we define:
msg3: hello\n world
What effect will it have? After my experiment, the effect is actually the same as adding single quotes - escape characters are not recognized.
Additionally, even if we don’t use the @Value
and @ConfigurationProperties
annotations, there are other ways to get parameters from the configuration in the code at any time. We can use the getProperty(parameter_name)
method in the Environment
class, for example:
@Autowired
private Environment env; //import org.springframework.core.env.Environment;
@Test
void test4(){
System.out.println(env.getProperty("msg1"));
}
- The parameter name can be used directly as a string, without adding
$
or{}
. Environment
should importorg.springframework.core.env.Environment;
- don’t import the wrong one.
Profile Usage
When developing SpringBoot applications, the same set of programs is usually installed in different environments, such as: development, testing, production, etc. The database address, server port, and other configurations are all different. If the configuration file has to be modified every time it’s packaged, it would be very troublesome. The profile function is used for dynamic configuration switching.
Multiple Profile Files Method
As shown in the figure below, I now create the following three YAML files:
I write in the application-dev.yml
file:
server:
port: 8082
Then I write in the application-test.yml
file:
server:
port: 8081
If I don’t write anything in application.yml
, the result after running is as follows:
You can see that no Profile is currently set, and it’s running on port 8080.
If I then write the following statement in application.yml
:
spring:
profiles:
active: dev
The result after running is as follows:
You can see that the Profile is now set, and dev
is currently active. The project is running on port 8082, which means the configuration in application-dev.yml
has taken effect. Similarly, if I change application.yml
to:
spring:
profiles:
active: test
Then the project will run on port 8081, switching to activate the test
Profile, meaning the configuration in application-test.yml
takes effect.
Through this method, we can quickly switch configurations suitable for different environments.
YAML Multi-Document Method
In the multiple Profile files method, we found that we need to create multiple yml
or properties
files, which is somewhat troublesome. So can we write configurations for different environments in the same configuration file, separated by special symbols? The answer is yes, as follows:
---
server:
port: 8081
spring:
profiles: test
---
server:
port: 8082
spring:
profiles: dev
---
spring:
profiles:
active: dev
Different configuration files only need to be separated by ---
.