Springboot3.5.x版本从环境变量加载属性

394 阅读1分钟

前言

Springboot3.5.x版本已经发布了,伴随着新属性的发型,能从环境变量加载属性

从环境变量加载属性

Springboot3.5.x版本之前,已经可以从环境变量加载单个属性,但现在,Spring Boot 3.5.x版本开始,可以从单个环境变量加载多个属性

备注: 本文使用的是window配置变量 1、进入到环境变量

image.png 2、然后在全局环境下配置

image.png 3、创建一个springboot3.5.x版本

image.png 4、配置环境变量

spring.config.import=env:USER_CONFIGURATION

image.png 5、写一个获取配置变量接口


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class IndexController {


    @Value("${user.age}")
    private Integer age;

    @GetMapping("/hello")
    public Integer hello() {
        return age;
    }
}

6、启动程序

image.png 7、访问

http://localhost:8085/hello

结果如下图

image.png 备注: 如果idea已经启动了,而后配置的环境变量,要重启idea

总结

Springboot3.5.x版本已经发布了,而且伴随着一些新属性使用,可以在开发中使用,方便开发,不过看个人技术选型。Springboot4.0.0跟快也要发版