Remember yML configuration files from the previous section? It works very well, but there are a lot of problems in actual production.

  1. Testing is a set of configurations, production is a set of configurations; Different people develop different modules, sharing a YML file is easy to conflict;
  2. Local development requires local configuration debugging. In this section, we will solve these headaches.

First we set up a project and change the application.properties file suffix to application.yml.

name: Zhang SAN
lesson: springboot
desc: ${lesson} ${lesson} ${lesson} # placeholders
Copy the code

DemoController.java

@RestController
@RequestMapping("demo")
public class DemoController {

    @Resource
    private YmlData ymlData;

    @GetMapping("test")
    public void test(a) throws JsonProcessingException { System.out.println(ymlData.getDesc()); }}Copy the code

YmlData.java

@Data
@Component
public class YmlData {

    @Value("${name}")
    private String name;
    @Value("${lesson}")
    private String lesson;
    @Value("${desc}")
    private String desc;
}
Copy the code

Browser requests under http://localhost:8080/demo/test look at the console, the code

Set the encoding format,ide-->file-->setting-->editor-->file encodings

After the setting is complete, the application. Yml is completely garbled, re-input, change the restart, request again, the console output:

Zhang SAN is learning springboot hard!!Copy the code

What does this have to do with multi-environment configurations?

Well, that’s fine, but you’ve learned one more yML file placeholder, and our following example will change accordingly.

application-prod.yml

name: Zhang SAN prod
lesson: springboot
desc: ${name} ${lesson}!!
Copy the code

application-dev.yml

name: Zhang SAN dev
lesson: springboot
desc: ${name} ${lesson}!!
Copy the code

application.yml

spring:
  profiles:
    active: dev The essence of multiple environment variables, here to determine which configuration file to load!!
Copy the code

Ok, now restart, after performing a request, what does the console output?

Dev is working hard on springboot!!Copy the code

Is it possible to choose which configuration file to load during debugging? , as shown in the following figure

Reboot and see what’s printed now? Yes, it’s printed.

Prod is working hard to learn Springboot!!Copy the code

Add another application-test.yml

name: Zhang SAN's test
Copy the code

Modify application.yml as follows:

spring:
  profiles:
    include: test,prod # Which configurations are included
    active: dev The essence of multiple environment variables, here to determine which configuration file to load!!
Copy the code

Restart the project to see what prints out?

Prod is working hard to learn Springboot!!Copy the code

From the result, we can see that the configuration that includes include and the same configuration is the last one to take effect in the file that includes include.

Active files that have the same configuration (the name configuration in this article) are not overwritten, and include profiles that do not have the same configuration are added to Active.

To sum up: include and active are union, with the same configuration as the last one in include!

More Java original reading: Javawu.com