yaml配置注入

参考视频:【狂神说Java】SpringBoot最新教程IDEA版通俗易懂 点击观看

文章目录


配置文件

SpringBoot使用一个全局的配置文件,配置文件名称是固定的。

application.properties

语法结构:key=value

application.yaml

语法结构:key:空格 value

配置文件的作用

可以修改SpringBoot自动配置的默认值(比如可以在配置文件中修改Tomcat默认启动的端口号)

复制代码
server.port=8081

yaml

yaml可以直接给实体类赋值

Person实体类

java 复制代码
package com.findx.helloworld.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;
@NoArgsConstructor
@AllArgsConstructor
@Data
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private Integer age;
    private Boolean happy;
    private Date birth;
    private Map<String, Object> maps;
    private List<Object> lists;
    private Dog dog;

}

yaml文件

yaml 复制代码
person:
  name: findx
  age: 23
  happy: true
  birth: 2003/01/08
  maps:
    k1: v1
    k2: v2
  lists:
    - 1
    - 2
    - 3
  Dog:
    name: fx
    age: 3

测试类

java 复制代码
package com.findx.helloworld;

import com.findx.helloworld.pojo.Dog;
import com.findx.helloworld.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
//单元测试
@SpringBootTest
class HelloworldApplicationTests {
    @Autowired
    private Dog dog;
    @Autowired
    private Person person;

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

properties

乱码问题

person.properties

php 复制代码
name=福鑫
age=18

Person实体类

java 复制代码
package com.findx.helloworld.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;
@NoArgsConstructor
@AllArgsConstructor
@Data
@Component
//@ConfigurationProperties(prefix = "person")
//加载指定的配置文件
@PropertySource(value = {"classpath:person.properties"})
public class Person {
    @Value("${name}")
    private String name;
    @Value("${age}")
    private Integer age;
    private Boolean happy;
    private Date birth;
    private Map<String, Object> maps;
    private List<Object> lists;
    private Dog dog;
}

测试类

java 复制代码
package com.findx.helloworld;

import com.findx.helloworld.pojo.Dog;
import com.findx.helloworld.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
//单元测试
@SpringBootTest
class HelloworldApplicationTests {

    @Autowired
    private Person person;

    @Test
    void contextLoads() {
        System.out.println(person);
    }
}
相关推荐
源码宝14 小时前
MES系统源码:Java8 + SpringBoot2.7 + MySQL8 + Redis,后端源码清爽易扩展
java·后端·源码·springboot·mes系统·源码二开·mes源码
MaCa .BaKa1 天前
55-宠物爱心救助领养系统-宠物救助领养系统
java·vue.js·tomcat·maven·springboot·宠物救助领养系统
苏渡苇1 天前
Spring Cloud Gateway 网关限流
spring cloud·gateway·springboot·网关限流
段ヤシ.1 天前
回顾Java知识点,面试题汇总Day17(持续更新)
java·springboot·spring security·shiro·mybatis-plus·jdbctemplate·spring data jpa
苦逼的猿宝2 天前
洗衣店订单管理系统(源码+论文)
java·毕业设计·springboot·计算机毕业设计
苦逼的猿宝2 天前
宠物咖啡馆平台的设计与实现(源码+论文)
java·毕业设计·springboot·计算机毕业设计
苦逼的猿宝2 天前
医院管理系统.(源码+论文)
java·毕业设计·springboot·计算机毕业设计
谷哥的小弟2 天前
图文详解Spring Boot整合MyBatisPlus(附源码)
mybatis·源码·springboot·mybatis-plus·整合
苏渡苇2 天前
微服务间的远程接口调用:OpenFeign 的使用
spring cloud·微服务·架构·springboot·openfeign·sca
better_liang3 天前
每日Java面试场景题知识点之-SpringBoot启动流程
java·面试·springboot·源码解析·启动流程