application.properties 配置文件

使用 SprongInitializr 方式创建一个名为 chapter02 的 SpringBoot 项目,在 Dependencies 依赖中选择 Web 依赖。
包结构为 com.itheima

在 com.itheima 包下创建一个 domain 包,并在该包下创建 Pet 和 Person 两个实体类

Pet.java

package com.itheima.chapter02.domain;

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

Person.java

package com.itheima.chapter02.domain;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.*;

@Component
@ConfigurationProperties(prefix = "person")


public  class Person {
    private int id;
    private String name;
    private List hobby;
    private String[] family;
    private Map map;
    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 getHobby() {
        return hobby;
    }

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

    public String[] getFamily() {
        return family;
    }

    public void setFamily(String[] family) {
        this.family = family;
    }

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    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=" + Arrays.toString(family) +
                ", map=" + map +
                ", pet=" + pet +
                '}';
    }
}

打开 chapter02 的 resources 目录下的 application.properties 配置文件进行属性配置

person.id=1
person.name=tom
person.hobby=play.read.sleep
person.family=father,mother
person.map.k1=v1
person.map.k2=v2
person.pet.type=dog
person.pet.name=kity

配置完成后,还需要在 pom.xml 文件中添加依赖

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

添加依赖之后,使用“Ctrl+F9”重构当前 SpringBoot 项目,然后在项目的 chapter02 的测试类 Chapter02ApplicationTests 中引入 Person 实体类 Bean,并进行输出测试

Chapter02ApplicationTests.java

package com.itheima.chapter02;

import com.itheima.chapter02.domain.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest

public class Chapter02ApplicationTests {

    @Autowired
    private Person person;

    @Test
    public void contextLoads() {
        System.out.println(person);
    }
}

运行 contextLoads()方法,输出结果如下:

Person{id=1, name='tom', hobby=[play, read, sleep], family=[father, mother], map={k1=v1, k2=v2}, pet=Pet{type='dog', name='kity'}}

application.yaml 配置文件

接下来,我们将使用 application.yaml 为 Person 对象赋值
在 chapter02 项目的 resources 目录下,新建一个 application.yaml 配置文件

person:
  id: 2
  name: 张三
  hobby: [sing,read,sleep]
  family: [father,mother]
  map: {k1: v1,k2: v2}
  pet: {type: cat, name: tom}

注意:yaml 文件的 “:” 后面有一个空格
在 Chapter02ApplicationTests 中 再次执行 contextLoads() 结果如下:

使用 @value 注入属性

在 com.itheima.domain 包下新建一个实体类 Student,并使用 @value 注解注入属性 id 和 name

Student.java

package com.itheima.chapter02.domain;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.awt.List;
import java.util.*;

@Component
public class Student {
    @Value("${person.id}")
    private int id;
    @Value("${person.name}")
    private String name;
    private List hobby;
    private String[] family;
    private Map map;
    private Pet pet;


    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", hobby='" + hobby +
                ", family='" + Arrays.toString(family) +
                ", map='" + map +
                ", pet='" + pet +
                '}';
    }

}

在 Chapter02ApplicationTests 中引入 Student 实体类 Bean,新增一个测试方法进行输出测试

@Autowired
private Student student;

@Test
public void studentTest() {
System.out.println(student);
}

运行结果如下:

Person{id=2, name='张三', hobby=null, family=null, map=null, pet=null}

注意:使用 @value 注入的属性类型只能是基本数据类型

最后修改:2024 年 01 月 18 日
如果觉得我的文章对你有用,请随意赞赏