Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 从Spring boot主类读取application.properties值_Spring Boot - Fatal编程技术网

Spring boot 从Spring boot主类读取application.properties值

Spring boot 从Spring boot主类读取application.properties值,spring-boot,Spring Boot,在我的spring boot应用程序中,我在application.properties中设置了变量值。 我想在主java类中阅读它。我该怎么做?下面的代码返回null @SpringBootApplication public class MyApplication { @Value( "${spring.shutdown.sleep.time}" ) private static String shutdownSleepTime; public static void main(Stri

在我的spring boot应用程序中,我在application.properties中设置了变量值。 我想在主java类中阅读它。我该怎么做?下面的代码返回null

@SpringBootApplication
public class MyApplication {

@Value( "${spring.shutdown.sleep.time}" )
private static String shutdownSleepTime;

public static void main(String[] args) {
    ConfigurableApplicationContext applicationContext = SpringApplication.run(MysApplication.class, args);
    System.out.println("sleep time : " + shutdownSleepTime);
我推荐它

@springboot应用程序
公共类MyApplication{
公共静态void main(字符串[]args){
ConfigurableApplicationContext applicationContext=SpringApplication.run(MysApplication.class,args);
}
@组成部分
公共静态类CmdRunner实现CommandLineRunner{
@值(${spring.shutdown.sleep.time}”)
私有静态字符串关闭睡眠时间;
公共无效运行(字符串…参数)引发异常{
System.out.println(“睡眠时间:+关机睡眠时间”);
}
}

您可以从applicationContext中读取属性

public static void main(String[] args) {
    ConfigurableApplicationContext applicationContext = SpringApplication.run(MysApplication.class, args);
    String shutdownSleepTime = applicationContext.getEnvironment().getProperty("spring.shutdown.sleep.time");
}

因此,您希望在创建应用程序上下文时打印一些内容

为此,您可以使用事件侦听器:


@SpringBootApplication
public class MyApplication {

  @Value( "${spring.shutdown.sleep.time}" )
  private String shutdownSleepTime; // not static

  public static void main(String[] args) {
      ConfigurableApplicationContext applicationContext = 
  SpringApplication.run(MysApplication.class, args);

  }

  @EventListener
  public void onAppContextStarted(ApplicationStartedEvent e) {
      System.out.println("sleep time : " + shutdownSleepTime); 
  }
}