gpt4 book ai didi

Spring Boot 应用程序在主方法中从属性文件中读取值

转载 作者:行者123 更新时间:2023-12-02 04:47:50 25 4
gpt4 key购买 nike

我正在尝试获取属性的值

hello.world=Hello World

在MainApp类中

@SpringBootApplication
public class MainApp {
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}

这作为主要方法不起作用。

@Value("${hello.world}")
public static String helloWorld;

也许可以通过以下方式加载

  Properties prop = new Properties();

// load a properties file
prop.load(new FileInputStream(filePath));

在SpringApplication.run之前,有没有其他更好的方法可以在SpringBoot的main方法中使用Spring获取属性

最佳答案

ConfigurableApplicationContext ctx = 
SpringApplication.run(MainApp.class, args);
String str = ctx.getEnvironment().getProperty("some.prop");
System.out.println("=>>>> " + str);

关于Spring Boot 应用程序在主方法中从属性文件中读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48155833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com