springboot加载配置文件几种方式

一,springboot优势之一就是自动化配置

开发过程中springboot自动读取application.properties和application.yaml,我们可以利用这一点实现配置读取

二,say nothing without codes

1,@Value(),最常用的方式

application.yaml

arduino 复制代码
config: zhang

代码里使用如下:

kotlin 复制代码
    @Value("${config}")
    private String config;

    
    @PostMapping("/index")
    public void say() {
        System.out.println(config);
        
    }

输出

2,配置成实体

以上用法有个缺点,当某个配置有过多属性,比如邮件,有名称,密码,地址等属性,Value过多,代码过于丑陋,那么实体就是个很好的解决方式

yaml 复制代码
config2:
  name: demo1
  value: demoValue1
kotlin 复制代码
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "config2")
public class BeanConfig {
    public String name;
    public String value;

}
kotlin 复制代码
@RestController
public class Controller {

    @Autowired
    public BeanConfig config2;

    @PostMapping("/index")
    public void say() {
        
        System.out.println(config2);
        
    }
}

输出:BeanConfig(name=demo1, value=demoValue1)

3,进阶版,配置成Map

yaml 复制代码
config3:
  -id1:
    name: demo1
    value: demoValue1
  -id2:
    name: demo2
    value: demoValue2
typescript 复制代码
@Data
@Component
@ConfigurationProperties(prefix = "config3")
public class Config3 {

    private Map<String, String> id1 = new HashMap<>();
    private Map<String, String> id2 = new HashMap<>();

    public Map<String, String> getId1() {
        return id1;
    }

    public void setId1(Map<String, String> id1) {
        this.id1 = id1;
    }

    public Map<String, String> getId2() {
        return id2;
    }

    public void setId2(Map<String, String> id2) {
        this.id2 = id2;
    }
}

输出:Config3(id1={name=demo1, value=demoValue1}, id2={name=demo2, value=demoValue2})

三,总结

以上配置基本满足日常需求

如果第三种也是可以配置成动态的方式,配置List里新增配置项可以自动识别。

yaml 复制代码
config4:
  maillist:
    - enable: true
      name: demo1
      value: demoValue1
    - enable: true
      name: demo2
      value: demoValue2
    - enable: true
      name: demo3
      value: demoValue3
less 复制代码
@Data
@Component
@ConfigurationProperties(prefix = "config4")
public class Config4 {
    private List<MailBean> mailList;
}


@Data
public class MailBean {
    public String name;
    public String value;
}

输出:Config4(mailList=[MailBean(name=demo1, value=demoValue1), MailBean(name=demo2, value=demoValue2), MailBean(name=demo3, value=demoValue3)])

相关推荐
Soofjan27 分钟前
Go 内存回收-GC 源码1-触发与阶段
后端
shining30 分钟前
[Golang]Eino探索之旅-初窥门径
后端
掘金者阿豪34 分钟前
Mac 程序员效率神器:6 个我每天都在用的 Mac 工具推荐(Alfred / Paste / PixPin / HexHub / iTerm2 /)
后端
小村儿1 小时前
连载04-CLAUDE.md ---一起吃透 Claude Code,告别 AI coding 迷茫
前端·后端·ai编程
han_hanker1 小时前
springboot 一个请求的顺序解释
java·spring boot·后端
2501_921649491 小时前
原油期货量化策略开发:历史 K 线获取、RSI、MACD 布林带计算到多指标共振策略回测
后端·python·金融·数据分析·restful
杰克尼1 小时前
SpringCloud_day05
后端·spring·spring cloud
ServBay1 小时前
阿里超强编程模型Qwen 3.6 -Plus 发布,国产编程AI的春天?
后端·ai编程
用户8356290780511 小时前
使用 Python 自动生成 Excel 柱状图的完整指南
后端·python
希望永不加班1 小时前
SpringBoot 静态资源访问(图片/JS/CSS)配置详解
java·javascript·css·spring boot·后端