Spring Boot basic study notes 03: Spring Boot two global configurations and two annotations

1. Application.properties configuration file

1. Add relevant configuration in application.properties

  • Click on the resource directory to view the application property configuration file
  • Configure tomcat port number and web virtual path
#Modify the default port number of tomcat
server.port=8888
#Modify the web virtual path
server.servlet.context-path=/lzy

2. Configuration and use of object types

(1) Create the Pet class

package lesson3;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;


public class Pet {
    private String type; //Types of
    private String name; //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 the Person class

package lesson3;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;


public class Person {
    private int id;
    private String name; //Name
    private List hobby; //Hobby
    private Map family; //family member
    private Pet 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 getHobby() {
        return hobby;
    }

    public void setHobby(List hobby) {
        this.hobby = hobby;
    }

    public Map getFamily() {
        return family;
    }

    public void setFamily(Map 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 +
                '}';
    }
}


(3) Configure objects in application.properties

#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 dog
person.pet.name=Rarity

(4) Add annotations to the Person class

  • 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

(5) Add annotations to the Pet class

(6) Obtain the actual column of the person class and output it

package lesson3;

import org.junit.jupiter.api.Test;
import org.springframework.beans.BeansException;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

@SpringBootTest
class Lesson3ApplicationTests implements ApplicationContextAware {

    private static ApplicationContext context;

    @Test
    void contextLoads() {
        Person person = (Person) context.getBean("person");
        System.out.println(person);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
}


  • Run the test class

(7) Solve the problem of garbled Chinese characters in the output result

  • Use the JDK tool native2ascii.exe to process Chinese characters into uncode encoding
  • Modify the application.properties file, Chinese characters adopt unicode encoding
#configuration object
person.id=1
person.name=\u5f20\u4e09\u4e30
person.hobby=\u65c5\u6e38,\u7f8e\u98df,\u97f3\u4e50
person.family.father=\u5f20\u4e91\u5149
person.family.mother=\u5434\u6587\u71d5
person.family.grandpa=\u5f20\u5b8f\u5b87
person.famliy.grandma=\u5510\u96e8\u6b23
person.family.son=\u5f20\u541b\u5b9d
person.family.daughter=\u5f20\u6653\u654f
person.pet.type=\u6cf0\u8fea\u72ac
person.pet.name=\u745e\u745e

  • operation result

(8) Get an instance of the Pet class from the Spring container and output it

  • Add the test method testPet() in the test class
  • run test

(9) Add the value annotation @Value to the properties of the Pet class

- configure application.properties

- operation result:

3. Comparison of two attribute annotation methods

  • With the @ConfigurationProperties annotation method, a set method is required to automatically inject corresponding values ​​for all properties of the annotated class, including simple types and complex types (List, Map, Pet...).
  • The advantage of using the @Value annotation is that the set method is not required, but there are two disadvantages: first, it needs to be injected one by one, which is troublesome; second, it cannot be injected into complex types, such as Map, List, Pet, etc.

2. Application.yaml configuration file

1. Create the application.yaml file in the resoures directory

#configure server
server:
  port: 8888
  servlet:
    context-path: /lzy

#Configure the person object
person:
  id: 1
  name: Zhang Sanfeng
  hobby:
    travel
    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

  • Run the test method contextLoads()
  • Run the test method testPet()

3. Comparison of two configuration files

1. application.properties configuration file

  • Using XML syntax, key-value pairs: key=value, no hierarchy
  • If there are Chinese characters in the value, it must be converted into unicode, otherwise there will be garbled characters

2. application.yaml configuration file

  • Using YAML syntax, key-value pairs: key: value (there is a space between the colon and the value), with a hierarchical structure
  • There are Chinese characters in the allowable value, and there is no need to convert them into unicode, and there will be no garbled characters

Tags: Spring Boot

Posted by nodi on Mon, 19 Dec 2022 08:21:53 +0300