Spring Boot two global configurations and two annotations

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

  1. Master the application.properties configuration file
  2. Master the application.yaml configuration file
  3. Mastering Injecting Properties Using @ConfigurationProperties
  4. 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

Please add a picture description

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 + ''' +
                '}';
    }
}

Please add a picture description

(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() {<!-- -->

    }
}

Please add a picture description

(3) Check code

Please add picture description

(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

Please add picture description

(5) Add annotations to the Person class

Add annotations @Component and @ConfigurationProperties(prefix = “person”), and hand it over to Spring for management

Please add picture description

–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

Please add a picture description

(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

Please add a picture descriptionIf the encoding is inconsistent, it will appearPlease add a picture description
Normal is this
Please add a picture description

Clear the red prompt box

Add a dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

Please add picture description

(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);

    }

Please add picture description

Another commentPlease add a picture description

Please add picture description

Please add picture description

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

Please add a picture description

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

Please add a picture description

Please add a picture description