简体   繁体   English

SpringBootApplication无法从application.yml文件加载属性

[英]SpringBootApplication failed to load properties from application.yml file

Configuration class, 配置类,

@Configuration
public class SpringContext {
@Bean
public BlockingQueue<String> queue(@Value("${queue.size}") int queueSize) {
    return new LinkedBlockingQueue<>();
   }
}

Main class, 主要班级,

@SpringBootApplication
public class SpringContextTest {

    public static void main(String[] args) {
        final SpringApplication springApplication = new SpringApplication(SpringContext.class);
        springApplication.setWebEnvironment(false);
        springApplication.run();
        System.out.println("queue.size" + System.getProperty("queue.size"));
    }

}

application.yml, application.yml,

queue.size: 10

While starting the main class I'm getting the following error, 在启动主类时,我收到以下错误,

Caused by: java.lang.NumberFormatException: For input string: "${queue.size}"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_144]

I'm I missing some annotations ?, In my understanding I've used the minimal annotations required for a spring boot application. 我是否缺少一些注释 ?在我的理解中,我使用了Spring启动应用程序所需的最小注释。 I have seen some similar posts but didn't helped. 我看过一些类似的帖子,但没有帮助。 Also tried with --spring.config.location . 还尝试使用--spring.config.location

My Spring starter version: 1.3.6.RELEASE 我的Spring入门版: 1.3.6.RELEASE

Your config file looks more like an application.properties rather than an application.yml 您的配置文件看起来更像是application.properties而不是application.yml

queue.size: 10

The equivalent yml should be: 等效的yml应该是:

queue:
    size: 10

UPDATE UPDATE

Yes both should work in .yml you are right. 是的,两者都适用于.yml你是对的。 I replicated exactly your example and it worked! 我完全复制了你的例子并且它有效!

Just make sure you application.yml file is in the root of the src/main/resources/ . 只需确保application.yml文件位于src/main/resources/的根目录中。 I had the same error as yours when I had the application.yml file in a subdirectory eg src/main/resources/com/myapp/ 当我在一个子目录中有application.yml文件时,我遇到了与你相同的错误,例如src/main/resources/com/myapp/

The Externalized Configuration section of the Spring Boot docs, explains all the details that you might need. Spring Boot文档的Externalized Configuration部分解释了您可能需要的所有细节。

As per your example to load properties in the main class you can do something like this, 根据您的示例在主类中加载属性,您可以执行以下操作,

  1. First double check the location of the yaml file, It should be located in /src/main/resources/application.yaml 首先仔细检查yaml文件的位置,它应该位于/src/main/resources/application.yaml
  2. The content should be like the following example, 内容应该类似于以下示例,

    \napp: 应用:\n    value1: 12 值1:12\n    value2: stringValue value2:stringValue\n
  3. Sample code, 示例代码,

    \npackage com.demo; 包com.demo;\n\nimport org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.core.env.Environment; import org.springframework.core.env.Environment;\n\n@SpringBootApplication @SpringBootApplication\npublic class App { 公共类App {\n\n
     public static void main( String[] args ){ SpringApplication app = new SpringApplication(App.class); Environment env = app.run(args).getEnvironment(); String value1 = env.getProperty("app.value1"); String value2 = env.getProperty("app.value2"); System.out.println("---------------- "+value1); System.out.println("---------------- "+value2); } 
    \n\n} }\n\n

Guesses 猜测

According to the current information you provides, I can't re-produce the problem, so the following is just some guesses: 根据您提供的当前信息,我无法重新产生问题,因此以下只是一些猜测:

  • Check whether file name and file location of application.yml is right; 检查application.yml文件名和文件位置是否正确;
  • Try use Spring EL: 尝试使用Spring EL:

     @Value("#{applicationConfig['queue.size']}") 
  • Try debug property loading: 尝试调试属性加载:

     @Bean public BlockingQueue<String> queue(ConfigurableEnvironment env) { return new LinkedBlockingQueue<>(); // set breakpoint here, to see if env has your property in PropertySource: applicationConfig } 

More 更多

More about property source and yaml loading in my blog. 更多关于我的博客中的属性源和yaml加载

Further 进一步

If the suggestions not help, you may better 如果建议没有帮助,你可能会更好

  • provide a sample project that can re-produce problem; 提供可以重新产生问题的示例项目;
  • provide complete stacktrace; 提供完整的堆栈跟踪;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从 application.yml 加载属性未绑定 - Failed to load properties from application.yml were left unbound 应用程序无法加载 application.yml 文件 - Application failed to load application.yml file 无法从位置“ classpath:/application.yml”加载属性源 - Failed to load property source from location 'classpath:/application.yml' 如何从 application.yml 文件注入属性? - How to inject properties from application.yml file? 在初始化SpringBootApplication之前寻找一种方法来解析springboot application.yml文件中的值 - Looking for a way to parse values in springboot application.yml file before SpringBootApplication is initialized 如何从 spring-boot 应用程序中的 application.yml 文件中读取属性 - How to read properties from application.yml file in spring-boot application 将 application.yml 文件转换为 application.properties - convert application.yml file to application.properties java.lang.IllegalStateException:无法从位置“类路径:/application.yml”加载属性源 - java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.yml' Spring Boot - 从 application.yml 和 application.properties 注入 - Spring Boot - inject from application.yml and application.properties java 8 Springboot 如何在注释中使用 application.yml 文件中的属性? - java 8 Springboot How to use properties from application.yml file in annotations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM