configuration file
There are two configuration file formats for SpringBoot. You can choose one of them. yaml is officially recommended:
- Format of application.properties key=value
- application.yaml key: value format
Profile location
SpringBoot loads the main configuration file from all four locations; complementary configuration. Priority is from high to low.
- --spring.config.location=F:/application.yaml
- /config/application.yaml
- /application.yaml
- /src/main/resources/config/application.yaml
- /src/main/resources/application.yaml
Features of yaml format
- There must be a space after the colon, which cannot be omitted
- Use indent to control hierarchy, and the left aligned ones are the same hierarchy
- Case sensitive properties and values
- Special characters in double quotation marks will escape output, such as "hello \n world" will break
- Special characters in double quotation marks will be output as they are, as seen in 'hello \n world'
Configuration example
# src/main/resources/application.properties server.port=8081
# src/main/resources/application.yaml server: port: 8081 # Object form student: name: zhangsan age: 18 # In line object form student: {name: zhangsan,age: 18} # Array form pets: - cat - dog - pig # In row array form pets: [cat,dog,pig]
Configure banner
# src/main/resources/banner.txt # https://www.bootschool.net/ascii-art _ _ _(9(9)__ __/^\/^\__ /o o \/_ __\_\_/\_/_/_ \___, \/_ _\.' './_ _/\_ `---`\ \/_ _\/ \/_ _|.'_/ \ \/_\/ / \/_ |/ / \ `-' | ';_:' / /| \ \ .' /_/ |,___.-`', /`'---` /___/` /____/
Inject yaml configuration file (mode 1)
package com.wu.helloworld.pojo; @Component public class Dog { @Value("Huang") private String name; @Value("18") private Integer age; }
@SpringBootTest class HelloworldApplicationTests { @Autowired Dog dog; @Test public void contextLoads() { System.out.println(dog) } }
Inject yaml configuration file (mode 2)
<!-- Import profile processor dependency, reboot required IDE --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
package com.wu.helloworld.pojo; @Component public class Dog { private String name; private Integer age; //Constructor, get, set, toString and other methods }
@Component @ConfigurationProperties(prefix = "person") 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; //Constructor, get, set, toString and other methods }
person: name: wu_${random.uuid} age: ${random.int} happy: false birth: 2000/01/01 maps: {k1: v1,k2: v2} lists: - code - girl - music dog: name: ${person.dogName:Default name}_Prosperous wealth age: 1
@SpringBootTest class DemoApplicationTests { @Autowired Person person; @Test public void contextLoads() { System.out.println(person); } }
Inject properties profile
Set the encoding format of properties to UTF-8:
File / Settings / File Encodings / Properties Files / UTF-8 && √ Transparent native-to-ascii conversion
# src/main/resources/person.properties person.name=wu person.age=18 person.sex=male
@PropertySource(value = "classpath:person.properties") @Component public class Person { @Value("${person.name}") // Take value from configuration file private String name; @Value("#{9*2}") // #{SPEL} Spring expression private int age; @Value("male") // Literal private String sex; }
Loose binding
dog: first-name: Prosperous wealth age: 3 @Component public class Dog { private String firstName; // The configuration value of the horizontal bar can be bound private Integer age; }