SpringBootyml構成ファイルは変数に値を割り当てます

 

1. personクラスとCarクラスを作成し、注釈@ConfigurationProperties(prefix = "person")をpersonクラスに追加します。これは、このクラスのメンバー変数の値が構成クラスから注入されることを示します。personクラスのメンバー変数にはget / setメソッドが必要であることに注意してください。


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

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


@ConfigurationProperties(prefix = "person")
public class Person {
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public boolean isMarriage() {
        return isMarriage;
    }

    public void setMarriage(boolean marriage) {
        isMarriage = marriage;
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public List<String> getHobbit() {
        return hobbit;
    }

    public void setHobbit(List<String> hobbit) {
        this.hobbit = hobbit;
    }

    public Map<String, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    private String name;

    private Integer age;

    private double salary;

    private boolean isMarriage;

    private Car car;

    private List<String> hobbit;

    private Map<String, Object> maps;

    private Date birthDate;
}

public class Car {

    private String carName;

    private String carBrand;

    public String getCarName() {
        return carName;
    }

    public void setCarName(String carName) {
        this.carName = carName;
    }

    public String getCarBrand() {
        return carBrand;
    }

    public void setCarBrand(String carBrand) {
        this.carBrand = carBrand;
    }
}

 

2.personクラスのyml構成ファイルを作成します。

person:
  name: zhangsan
  age: 18
  salary: 8888.88
  car:
    carName: 奥迪A6L
    carBrand: 奥迪
  hobbit:
    - 篮球
    - rap
    - 唱歌
    - 保健
  maps:
    k1: v1
    k2: v2
  birthDate: 1991/08/21
  marriage: true

3.起動クラスを作成し、注釈@ EnableConfigurationProperties(Person.class)を追加して、起動時にこのクラスのメンバー変数を構成ファイルから挿入できることをPersonに通知します。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableConfigurationProperties(Person.class)
@RestController
public class Tulingspc01SpringbootPropertiesMappingApplication {

	@Autowired
	private Person person;

	public static void main(String[] args) {
		SpringApplication.run(Tulingspc01SpringbootPropertiesMappingApplication.class, args);
	}

	@RequestMapping("/getPersonInfo")
	public Person getPersonInfo() {
		return person;
	}

}

 試験結果:

 

おすすめ

転載: blog.csdn.net/pengweismile/article/details/95916998