[英]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, 根据您的示例在主类中加载属性,您可以执行以下操作,
The content should be like the following example, 内容应该类似于以下示例,
\napp:应用:\n value1: 12
值1:12\n value2: stringValue
value2:stringValue\n
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
\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
According to the current information you provides, I can't re-produce the problem, so the following is just some guesses: 根据您提供的当前信息,我无法重新产生问题,因此以下只是一些猜测:
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 about property source and yaml loading in my blog. 更多关于我的博客中的属性源和yaml加载 。
If the suggestions not help, you may better 如果建议没有帮助,你可能会更好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.