【问题标题】:Spring Boot application to read a value from properties file in main methodSpring Boot 应用程序在 main 方法中从属性文件中读取值
【发布时间】:2018-01-08 18:12:04
【问题描述】:

我正在尝试获取属性的值

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获取属性还有其他更好的方法吗

【问题讨论】:

  • 为什么在上下文运行之前需要这些信息?

标签: spring spring-boot


【解决方案1】:
ConfigurableApplicationContext ctx = 
           SpringApplication.run(MainApp.class, args);
String str = ctx.getEnvironment().getProperty("some.prop");
System.out.println("=>>>> " + str);

【讨论】:

  • 在这之后我们必须再次调用 app.run(args) 来启动应用程序?
  • 我们通常调用run()方法来启动spring boot应用。 run() 返回 ConfigurableApplicationContext,而 ConfigurableEnvironment 又可以返回包含拉取属性的方法。要验证查看您的SpringApplication.run(MainApp.class, args) 语句,它还将返回ConfigurableApplicationContext。
  • 如果我们必须在 SpringApplication.run 之前得到呢?
  • 当我们调用 SpringApplication.run() 时,它会启动自动连接依赖等任务。在此之前,在 spring 的帮助下,我们无法获取任何属性,要实现这一点,您必须阅读属性文件手动。您能否在run() 调用方法之前指定您要读取属性的用例是什么?
  • 那么我相信你必须按照你之前提到的手动过程来加载属性文件并阅读它。
【解决方案2】:

您已将变量 helloWorld 声明为静态。因此,您需要使用 Setter Injection 而不是 Field Injection。

注入静态非最终字段是一种不好的做法。因此 Spring 不允许这样做。但是你可以做一个这样的解决方法。

public static String helloWorld;

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

您可以在类中的任何位置访问此变量 helloWorld,如果它是任何其他类的话。但是如果你想在主课上做。您只能在此行之后访问变量

SpringApplication.run(MainApp.class, args);)

即仅在应用程序启动后。

【讨论】:

  • 当然可以。唯一的问题是我们只能在SpringApplication.run(....) 之后访问该变量
【解决方案3】:

不要这样做。最好使用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
}

}

【讨论】:

  • 让 Spring Boot 从 application.properties 获取属性 my-value 使用 @Value(${my-value}) myValue 不是,因为你有它 @Value("myvalue") myValue 这意味着 String myValue = "我的价值";
  • 谢谢@NetDawg,已在上面的答案中修复。
【解决方案4】:
@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 启动并且 Environment 首次可供检查和修改时发布的事件。

【讨论】:

    【解决方案5】:
    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);
    

    【讨论】:

    • 嗨,维沙尔。感谢您的回答。通常提供几句话来解释解决方案很有用。
    【解决方案6】:

    我们无法将值读入静态字段。这是解释提供了更好的洞察力How to assign a value from application.properties to a static variable?

    【讨论】:

      猜你喜欢
      • 2017-06-26
      • 2020-01-22
      • 1970-01-01
      • 2021-03-22
      • 2020-07-01
      • 2017-06-17
      • 2018-07-16
      • 2021-09-01
      • 1970-01-01
      相关资源
      最近更新 更多