Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

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

like image 303
Krishas Avatar asked Sep 16 '17 08:09

Krishas


1 Answers

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/

like image 95
pleft Avatar answered Sep 25 '22 05:09

pleft