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);
    }
}
相关推荐
小云小白17 小时前
PostgreSQL JDBC 驱动长连接问题:无心跳导致的静默断连
postgresql·springboot·连接超时
程序员老邢2 天前
【产品底稿 11】架构规整收官:从混乱到清晰,工程结构、表命名、模块分层一次性定型
后端·架构·springboot·产品底稿·架构规整·模块分层·数据库规范
@yanyu6662 天前
登录注册功能-明文
vue.js·springboot
苏渡苇4 天前
DeepSeek V4 实战:自然语言生成 SQL + 智能优化引擎
ai·springboot·spring ai·deepseek·ai推理·deepseek v4·自然语言生成sql
玛卡巴卡ldf4 天前
【Springboot升级AI】(大模型部署)LangChain4j、会话记忆、隔离消失持久化问题、ollama、RAG知识库、Tools工具
java·开发语言·人工智能·spring boot·后端·springboot
Nick_zcy4 天前
小说在线阅读网站和小说管理系统 · 功能全解析
java·后端·python·springboot·ruoyi
苏渡苇5 天前
DeepSeek V4 实战:打造一个智能 Java 项目源码分析助手
springboot·jdk21·spring ai·deepseek·deepseek v4
玛卡巴卡ldf6 天前
【Springboot9】将业务模块数据导出为PDF
pdf·springboot
阿冰冰呀7 天前
互联网大厂Java求职面试实录:谢飞机的“水货”之路
java·mybatis·dubbo·springboot·线程池·多线程·hashmap