This is the first day of my participation in the August More Text challenge
1. Purpose: in the code for the application. The properties/yml in data
1 Obtain single data
/ / 1
@Value("${spring.map.url}")
private String url;
/ / 3 calls
public void aVoid(){system.out. println(" path is "+ url); }Copy the code
Spring: map: url: 127.0.0.1:9000Copy the code
2 Match data in batches
/ / 1 class match
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Booleanboss; . Omit getset, tostring, etcCopy the code
#2 Config file person: lastName: Battle 3 Age: 12 boss: trueCopy the code
@autowired private Person Person; @RequestMapping("/test1") public void aVoid(){ System.out.println(person.toString()); }Copy the code
3 extension
// Matches the specified configuration file
@PropertySource(value = “classpath:person.yml”)
2 Purpose: Add a new component to SpringBoot
@configuration // Config class public Class MyAppConfig {// Add the return value of the method to the container. The default id of the bean in the container is the method name helloService @bean public helloService HelloService (){system.out.println (" Add to container "); return new HelloService(); }}Copy the code
3 Placeholder in YML
1 can write random numbers
1${random.uuid} age: 121${random.int}Copy the code
2 placeholder to get the value previously configured. If not, you can set the default value
abc: ${person.age:12}
Copy the code
4 Configuring multiple environments
4.1 Configuration in YML
Spring: Profiles: Active: test Currently is the test environmentCopy the code
Start configuration in 4.2 IDEA
5 Resours multiple configuration files
Yml is used as the default configuration file. Multiple configurations are loaded based on their priorities. The configuration with a higher priority overwrites the configuration with a lower priority.
From high —– to low
You can also change the default configuration by configuring spring.config.location.
6 Add components to the container based on the specified conditions.
You can set debug=true in YML to view the debug mode during springboot startup