Spring Boot Application.properties和yaml配置文件


一、全局配置文件概述

全局配置文件能够对一些默认配置值进行修改。Spring Boot使用一个application.properties或者application.yaml的文件作为全局配置文件,该文件存放在src/main/resource目录或者类路径的/config,一般会选择resource目录。

二、Application.properties配置文件

(一)创建Spring Boot的Web项目PropertiesDemo

使用Spring Initializr方式创建项目——PropertiesDemo
在这里插入图片描述
单击【Next】按钮
在这里插入图片描述

添加Web和DevTools依赖
在这里插入图片描述
单击【Create】按钮
在这里插入图片描述
设置项目编码为utf8(尤其注意复选框)
在这里插入图片描述

(二)在应用属性文件里添加相关配置

点开resource目录,查看应用程序属性配置文件
在这里插入图片描述

1、配置服务器端口号和web虚拟路径

在application.properties文件里配置
在这里插入图片描述

# 设置服务端口号
server.port=8888
# 设置web虚拟路径
server.servlet.context-path=/lzy

更多配置属性,详见官网

启动应用,查看控制台
在这里插入图片描述

2、对象类型的配置与使用

(1)创建Pet类

在net.army.boot里创建bean子包,在子包里创建Pet类
在这里插入图片描述

package net.army.boot.bean;

/**
 * 作者:梁辰兴
 * 日期:2023/5/31
 * 功能:宠物类
 */
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)创建Person类

在net.army.boot里创建bean子包,在子包里创建Person类

在这里插入图片描述

package net.army.boot.bean;

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

/**
 * 作者:梁辰兴
 * 日期:2023/5/31
 * 功能:人类
 */
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 +
                '}';
    }
}

(3)在应用属性文件里配置对象

配置Person对象属性
在这里插入图片描述

# 配置对象
person.id=1
person.name=张三丰
person.hobby=旅游,美食,音乐
person.family.father=张云光
person.family.mother=吴文燕
person.family.grandpa=张宏宇
person.famliy.grandma=唐雨欣
person.family.son=张君宝
person.family.daughter=张晓敏
person.pet.type=泰迪犬
person.pet.name=瑞瑞

(4)给Person类添加注解

添加注解@Component,交给Spring去管理
在这里插入图片描述
添加注解@ConfigurationProperties(prefix = “person”)
在这里插入图片描述

注意:采用@ConfigurationProperties注解方式,必须要有set方法,才会自动为Person类所有属性注入相应的值,包括简单类型和复杂类型

配置Spring Boot注解处理器,去掉红色光条里的提示信息
在这里插入图片描述

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

此时,红色框警告信息不见了
在这里插入图片描述

(5)给Pet类添加注解

添加注解@Component,交给Spring去管理
添加注解@ConfigurationProperties(prefix = “person.pet”) - 可以不用添加
在这里插入图片描述

(6)从Spring容器里获取Person类的实例并输出

实现接口ApplicationContextAware,实现其抽象方法setApplicationContext
在这里插入图片描述
声明ApplicationContext对象,并在setApplicationContext里初始化
在这里插入图片描述创建测试方法testPerson(),从Spring容器中获取Person类的实例并输出
在这里插入图片描述
运行测试方法testPerson(),查看结果
在这里插入图片描述查看测试类PropertiesDemoApplicationTests代码

package net.army.boot;

import net.army.boot.bean.Person;
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 PropertiesDemoApplicationTests implements ApplicationContextAware {
    
    

    private ApplicationContext context; // 应用容器

    @Test
    void contextLoads() {
    
    
    }

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

    @Test
    public void testPerson() {
    
    
        // 按名称从应用容器中获取Person对象
        Person person = (Person) context.getBean("person");
        // 输出Person对象
        System.out.println(person);
    }
}

(7)从Spring容器里获取Pet类的实例并输出

查看Pet类的注解,有配置属性的注解@ConfigurationProperties(prefix = “person.pet”)
在这里插入图片描述
在测试类里添加测试方法testPet()
在这里插入图片描述
运行测试方法testPet(),查看结果
在这里插入图片描述
注释掉Pet类的配置属性的注解@ConfigurationProperties(prefix = “person.pet”)
在这里插入图片描述再次运行测试方法testPet(),查看结果
在这里插入图片描述
修改应用属性文件,配置宠物对象
在这里插入图片描述再次运行测试方法testPet(),查看结果
在这里插入图片描述可以看到,宠物对象的属性依然没有被注入,下面我们换一种属性注解的方式,采用@Value注解方式。

给Pet类的属性添加值注解@Value
在这里插入图片描述
再次运行测试方法testPet(),查看结果
在这里插入图片描述

3、两种属性注解方式的对比

采用@ConfigurationProperties注解方式,必须要有set方法,才会自动为所注解的类的全部属性注入相应的值,包括简单类型和复杂类型(List、Map、Pet……)。

采用@Value注解方式,优点在于可以不要set方法,但是有两点不足:其一、需要一个一个地注入,显得麻烦;其二、对于复杂类型不能注入,比如Map、List、Pet等。

三、Application.yaml配置文件

1、备份application.properties文件

文件更名为application.back,即让此文件不起作用
在这里插入图片描述

2、创建application.yaml文件

在resoures目录里创建application.yaml文件
在这里插入图片描述
配置服务器属性
在这里插入图片描述

# 配置服务器
server:
  port: 8888 # 配置端口号
  servlet:
    context-path: /lzy # 配置虚拟路径

# 配置person对象
person:
  id: 1
  name: 张三丰
  hobby:
    旅游,
    美食,
    音乐
  family: {
    
    
    father: 张云光,
    mother: 吴文燕,
    grandpa: 张宏宇,
    grandma: 唐雨欣,
    son: 张君宝,
    daughter: 张晓敏
  }
  pet:
    type: 泰迪犬
    name: 瑞瑞

# 配置pet对象
pet:
  type: 泰迪犬
  name: 瑞瑞

3、运行测试方法testPerson(),查看结果

运行testPerson()方法
在这里插入图片描述

4、运行测试方法testPet(),查看结果

运行测试方法testPet()
在这里插入图片描述

四、两种配置文件的比较

1、application.properties配置文件

  • 采用XML语法,键值对:键=值,没有层次结构
  • 如果值里有汉字,必须得转成unicode,否则会出现乱码问题

2、application.yaml配置文件

  • 采用YAML语法,键值对:键: 值(冒号与值之间有空格),具有层次结构
  • 允许值里有汉字,不必转成unicode,也不会出现乱码问题

五、课后作业

任务:创建StudentInfo项目输出学生信息

在net.army.boot包里创建bean子包,在子包里创建Student类

添加属性

private String id;
private String name;
private String gender;
private int age;
private String major;
private String telephone;
private String email;
private List<String> hobbies;

添加getter和setter

添加toString()方法

添加注解@Component

添加注解@ConfigureProperties

将application.properties更名为application.yaml

在应用属性文件里配置student对象

修改控制器StudentInfoController,创建getStudent()方法,返回student对象

运行入口类,在浏览器里访问http://localhost:8080/getStudent

猜你喜欢

转载自blog.csdn.net/m0_62617719/article/details/130834003