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);
    }
}
相关推荐
十五喵源码网3 天前
基于springboot2+vue2的租房管理系统
java·毕业设计·springboot·论文笔记
whaledown4 天前
Kafka 与 Java 消息队列入门:用订单场景理解核心机制
java·kafka·消息队列·springboot
二哈赛车手4 天前
新人笔记---最终版智能体图片分析完整方案,包括一些总结于经验,以及各种优化点讲解
java·笔记·spring·ai·springboot
十五喵源码网4 天前
基于SpringBoot2+vue2的酒店客房管理系统
java·毕业设计·springboot·论文笔记
这里是杨杨吖5 天前
SpringBoot+Vue高校在线考试系统 附带详细运行指导视频
vue·在线考试·springboot
就改了6 天前
ElasticsearchRestTemplate使用方法详解!!!
java·elasticsearch·springboot
郑洁文11 天前
旅游景点推荐系统的设计与实现
springboot·毕设·旅游系统·旅游景点推荐系统
ANnianStriver12 天前
PetLumina-AI 驱动的宠物生活管理平台
java·生活·vue3·springboot·ai编程·宠物·全栈开发
YDS82912 天前
DeepSeek RAG&MCP + Agent智能体项目 —— 集成ELK日志管理系统和Prometheus监控系统
java·elk·ai·springboot·agent·prometheus·deepseek
极光代码工作室13 天前
基于SpringBoot的任务管理系统
java·springboot·web开发·后端开发