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);
    }
}
相关推荐
文慧的科技江湖1 天前
一文读懂光伏、储能,充电工作原理,是如何实现闭环的,其中微电网(ems)是什么;这个内容,就看懂了源网荷储。十五五规划里面最核心的能源管理内容
开源·springboot·储能·光伏
AI人工智能+电脑小能手1 天前
【大白话说Java面试题 第163题】【06_Spring篇】第23题:说说SpringBoot 自动配置原理?
java·自动配置·springboot·spi·条件注解
AI人工智能+电脑小能手1 天前
【大白话说Java面试题 第164题】【06_Spring篇】第24题:如何理解 SpringBoot 的 Starter?
java·自动配置·springboot·starter·自定义组件
极光代码工作室1 天前
基于SpringBoot的订单管理系统
java·springboot·web开发·后端开发
衍生星球4 天前
SpringBoot3 + Vue3 + 微信小程序如何学习,以及学哪些技术,组件
sql·微信小程序·uni-app·vue·springboot
她说..5 天前
Java 默认值设置方式
java·开发语言·后端·springboot
折哥的程序人生 · 物流技术专研7 天前
Java面试通关⑨:SpringBoot核心全集
自动配置·springboot·starter·校招·java面试·面试真题·内嵌容器
ShiXZ2137 天前
PDF-OCR文件识别篇(八):配置、运维与排错
java·运维·ocr·dubbo·springboot
ShiXZ2137 天前
PDF-OCR文件识别篇(七):数据入库
java·pdf·json·ocr·springboot
带刺的坐椅15 天前
Spring Boot → Solon 注解迁移实战指南:一张对照表说清楚
java·springboot·web·solon