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);
    }
}
相关推荐
成为大佬先秃头8 小时前
前后分离项目:整合JWT+Shiro
java·springboot·shiro·jwt
程序员老邢12 小时前
【产品底稿 04】商助慧 V1.1 里程碑:爬虫入库 + MySQL + Milvus 全链路打通
java·爬虫·mysql·ai·springboot·milvus
宠友信息2 天前
社交软件源码哪个渠道好
java·微服务·架构·社交电子·springboot·uniapp
玛卡巴卡ldf3 天前
【Springboot6】内存泄漏OOM、VisualVM、Arthas、Prometheus Grafana监控、垃圾回收
java·jvm·springboot
Flittly4 天前
【SpringAIAlibaba新手村系列】(15)MCP Client 调用本地服务
java·笔记·spring·ai·springboot
可以简单点6 天前
分析一个线程日志工具类
java·springboot
鬼先生_sir7 天前
SpringBoot-基础面试篇
java·springboot·java面试
成为大佬先秃头7 天前
日志框架:Logback
springboot·logback
Flittly9 天前
【SpringAIAlibaba新手村系列】(11)Embedding 向量化与向量数据库
java·笔记·spring·ai·springboot