Reputation: 1694
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,
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. I have seen some similar posts but didn't helped. Also tried with --spring.config.location.
My Spring starter version: 1.3.6.RELEASE
Upvotes: 7
Views: 12590
Reputation: 6148
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;Try use 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.
If the suggestions not help, you may better
Upvotes: 1
Reputation: 7905
Your config file looks more like an application.properties
rather than an application.yml
queue.size: 10
The equivalent yml
should be:
queue:
size: 10
UPDATE
Yes both should work in .yml
you are right. I replicated exactly your example and it worked!
Just make sure you application.yml
file is in the root of the src/main/resources/
. I had the same error as yours when I had the application.yml
file in a subdirectory e.g. src/main/resources/com/myapp/
Upvotes: 3
Reputation: 180
The Externalized Configuration section of the Spring Boot docs, explains all the details that you might need.
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,
app: value1: 12 value2: stringValue
Sample code,
package com.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.core.env.Environment; @SpringBootApplication public class App {}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); }
Upvotes: 1