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

6

我正在尝试获取属性的值

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));

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


在上下文运行之前,为什么需要这些信息? - Maciej Walkowiak
6个回答

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

这之后我们需要再次调用app.run(args)来启动应用程序吗? - Cork Kochi
通常我们调用 run() 方法来启动 Spring Boot 应用程序。run() 返回 ConfigurableApplicationContext,它可以返回包含拉取属性方法的 ConfigurableEnvironment。要验证,请查看您的 SpringApplication.run(MainApp.class, args) 语句,它也将返回 ConfigurableApplicationContext - sanit
如果在SpringApplication.run之前需要获取什么? - Cork Kochi
2
当我们调用SpringApplication.run()时,它会启动诸如自动装配依赖等任务。在此之前,我们无法使用Spring获取任何属性,为了实现这一点,您必须手动读取属性文件。请问您想在调用run()方法之前读取属性的使用场景是什么? - sanit
那么我相信你必须按照之前提到的手动流程来加载属性文件并读取它。 - sanit

3
你已经将变量helloWorld声明为静态的。因此,你需要使用Setter注入而不是Field注入。
注入静态的非final字段是不好的实践。因此,Spring不允许这样做。但是你可以像这样做一个解决方法。
public static String helloWorld;

      @Value("${hello.world}")
        public void setHelloWorld(String someStr) {
          helloWorld = someStr
        }

你可以在类的任何其他位置访问变量helloWorld。但是,如果你想在主类中访问它,你只能在以下行之后访问该变量:SpringApplication.run(MainApp.class, args); ,也就是说,只有在应用程序启动之后才能访问它。

2
当然可以工作。唯一的问题是我们只能在 SpringApplication.run(....) 之后访问变量。 - pvpkiran

2
不要这样做。最好使用CommandLineRunner。 有了它,您可以拥有一个非静态方法,Spring Boot会自动运行它:
@SpringBootApplication
public class SimulatorApplication implements CommandLineRunner {

@Value("${my-value}")
private myValue;

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

@Override
public void run(String... args) throws Exception {
  // here you can access my-value
}

}


1
为了让Spring Boot从application.properties获取属性my-value,请使用以下代码: @Value(${my-value}) myValue 而不是: @Value("myvalue") myValue 这意味着: String myValue = "my-value"; - Net Dawg
感谢@NetDawg,已在上面的答案中修复。 - Tomasz S

1
@SpringBootApplication
public class MainApp {
   public static void main(String[] args) {
     SpringApplication springApplication = new SpringApplication(MainApp.class);
     springApplication.addListeners(new VersionLogger());
     springApplication.run(args);
}


// The VersionLogger Class
public class VersionLogger implements ApplicationListener<ApplicationEnvironmentPreparedEvent>{

  @Override
  public void onApplicationEvent(ApplicationEnvironmentPreparedEvent applicationEvent) {
    String helloWorld = applicationEvent.getEnvironment().getProperty("hello.world");
  }
}

ApplicationEnvironmentPreparedEvent 当一个SpringApplication正在启动并且环境首次可用于检查和修改时,发布的事件。


1
ApplicationContext applicationContext = SpringApplication.run(Application.class, args);

String applicationPropertyVersion=applicationContext.getEnvironment().getProperty("application.property.version");

    LOGGER.info("RELEASE CODE VERSION {} and applicationProperty Version {} ", LcoBuildVersion.version,
            applicationPropertyVersion);

1
嗨,Vishal。感谢你的回答。通常最好提供一些解释来说明解决方案。 - MaxV

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接