Article directory
- Zero. Learning Objectives
- 1. Overview of the global configuration file
- Two, Application.properties configuration file
-
- 1. Create Spring Boot’s Web project PropertiesDemo
-
- (1) Create a project using Spring Initializr
- 2. Add relevant configuration in the application properties file
- 3. Configuration and use of object types
-
- (1) Create the Pet class
- (2) Create the Person class
- (3) Check code
- (4) Configure the object in the application properties file
- (5) Add annotations to the Person class
- (6) Add annotations to the Pet class
- (7) Obtain an instance of the Person class from the Spring container and output it
- Eliminate the red prompt box
- (8) Get an instance of the Pet class from the Spring container and output it
- Another comment! [Please add a picture description](https://img-blog.csdnimg.cn/a1c7aed70d544f7780bedac0849f8896.png)
- 3. Application.yaml configuration file
-
- 1. Back up the application.properties file
-
- –The file is renamed application.properties.ban, which means that this file does not work
- 2. Create the application.yaml file in the resoures directory
-
- — Create application.yaml file
Zero, learning objectives
- Master the application.properties configuration file
- Master the application.yaml configuration file
- Mastering Injecting Properties Using @ConfigurationProperties
- Mastering Injecting Attributes Using @Value
1. Overview of global configuration files
–The global configuration file can modify some default configuration values. Spring Boot uses an application.properties or application.yaml file as a global configuration file, which is stored in the src/main/resource directory or /config in the class path, and the resource directory is generally selected.
2. Application.properties configuration file
1. Create Spring Boot’s Web project PropertiesDemo
(1) Create a project using Spring Initializr
2. Add relevant configuration in the application properties file
# Set the service port number server.port=8888 # Set the web virtual path server.servlet.context-path=/lzy
3. Configuration and use of object types
(1) Create Pet class
– Create a bean subpackage in net.dw.boot, and create a Pet class in the subpackage
package net.dw.boot.bean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "person. pet") public class Pet {<!-- --> private String type; private String name; public String getType() {<!-- --> return type; } public void setType(String type) {<!-- --> this.type = type; } public String getName() {<!-- --> return name; } public void setName(String name) {<!-- --> this.name = name; } @Override public String toString() {<!-- --> return "Pet{" + "type='" + type + ''' + ", name='" + name + ''' + '}'; } }
(2) Create Person class
–Create the Person class in the net.dw.boot.bean package
package net.dw.boot.bean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; @Component @ConfigurationProperties(prefix = "person") public class Person {<!-- --> private int id; private String name; private List<String> hobby; private Map<String,String> family; private pet pet; public int getId() {<!-- --> return id; } public void setId(int id) {<!-- --> this.id = id; } public String getName() {<!-- --> return name; } public void setName(String name) {<!-- --> this.name = name; } public List<String> getHobby() {<!-- --> return hobby; } public void setHobby(List<String> hobby) {<!-- --> this. hobby = hobby; } public Map<String, String> getFamily() {<!-- --> return family; } public void setFamily(Map<String, String> family) {<!-- --> this. family = family; } public Pet getPet() {<!-- --> return pet; } public void setPet(Pet pet) {<!-- --> this. pet = pet; } @Override public String toString() {<!-- --> return "Person{" + "id=" + id + ", name='" + name + ''' + ", hobby=" + hobby + ", family=" + family + ", pet=" + pet + '}'; } public Person() {<!-- --> } }
(3) Check code
(4) Configure objects in the application properties file
# configuration object person.id=1 person.name=Zhang Sanfeng person.hobby=travel,food,music person.family.father=Zhang Yunguang person.family.mother=Wu Wenyan person.family.grandpa=Zhang Hongyu person.famliy.grandma=Tang Yuxin person.family.son=Zhang Junbao person.family.daughter=Zhang Xiaomin person.pet.type=Teddy person.pet.name=Rarity
(5) Add annotations to the Person class
–Add annotations @Component and @ConfigurationProperties(prefix = “person”), and hand it over to Spring for management
–Note: With the @ConfigurationProperties annotation method, the set method must be used to automatically inject corresponding values for all properties of the Person class, including simple types and complex types
(6) Add annotations to the Pet class
–Add annotations @Component and @ConfigurationProperties(prefix = “person.pet”), and hand it over to Spring for management
(7) Obtain an instance of the Person class from the Spring container and output it
– Implement the interface ApplicationContextAware and implement its abstract method setApplicationContext. And declare the ApplicationContext object, and initialize it in setApplicationContext.
Create a test method testPerson(), get an instance of the Person class from the Spring container and output
– If the encoding is inconsistent, it will appear
– Normal is this
Clear the red prompt box
–Add a dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
(8) Obtain an instance of the Pet class from the Spring container and output it
@Test public void testPet(){<!-- --> //Get the Pet object by name from the application container Pet pet = (Pet) context. getBean("pet"); //Output the Pet object System.out.println(pet); }
Another comment
3. Application.yaml configuration file
1. Backup application.properties file
–The file is renamed application.properties.ban, which means that this file does not work
2. Create the application.yaml file in the resoures directory
– Create application.yaml file
#configure server server: port: 8888 servlet: context-path: /lzy #Configure the person object person: id: 1 name: Zhang Sanfeng hobby: travel gourmet food music family: {<!-- --> father: Zhang Yunguang, mother: Wu Wenyan, grandpa: Zhang Hongyu, grandma: Tang Yuxin, son: Zhang Junbao, daughter: Zhang Xiaomin } pet: type: teddy dog name: Rarity #Configure the pet object pet: type: teddy dog name: Rarity