4
votes

I use Spring Boot. I would like to write my configuration using YAML instead of properties.

Since I use spring-boot-starter the SnakeYAML library is already in the classpath, and SpringApplication should use the YAML version automatically.

The SpringApplication class will automatically support YAML as an alternative to properties whenever you have the SnakeYAML library on your classpath.

The problem is that the application keep using the application.properties file and if I remove it no configuration are loaded at all.

Can someone help me? this is my main file

@SpringBootApplication
public class App {


    public static void main(String[] args) throws Exception {
        SpringApplication app = new SpringApplication(App.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
    }
}

this is my pom.xml

....
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

the application.yml file is just

tasks: 231232

and I try reading the property using the injected environment

     @Autowired
      private  Environment environment;

....

        log.info(environment.getProperty("tasks"));

Where is my mistake?

8
Can you try to create an attribute of type String which is annotated with @Value("${tasks}") and check its value? - Arnaud Denoyelle
everything is well documented boot-features-external-config - Patrick
Could not resolve placeholder 'tasks' in string value "${tasks}" - Panciz
(@Patrick) I know this is the documentation I'm using. - Panciz

8 Answers

4
votes

I solve my problem adding

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.16</version>

to my pom.xml file. Notice 1.16, the spring-boot-starter-parent imports 1.17 .

I open an issue https://github.com/spring-projects/spring-boot/issues/6878

2
votes

The problem was simply that snakeyaml 1.7 jar was corrupted in my local repository. Probably the class was not loaded at run time.

The fact that I do not recieve any error at build not at run time mislead me.

2
votes

If the specified version has conflicts, you can use this method to remove the version.

    <dependency>
        <groupId>org.yaml</groupId>
        <artifactId>snakeyaml</artifactId>
    </dependency>
1
votes

Did you add space after tasks: Make sure you add the space after colon ( : ). Also in yml indention plays a vital role

tasks: 123

Here a syntax page

1
votes

An update :

Spring Boot 2 has snakeyaml as a transitive runtime dependency for any of its modules.

enter image description here

1
votes

I was using gradle... I add this dependency without version name in my build.gradle (app level) and worked fine for me:

implementation ('org.yaml:snakeyaml') 
0
votes

use ConfigurationProperties annotation in main class

@SpringBootApplication
@ConfigurationProperties
public class App {


    public static void main(String[] args) throws Exception {
        SpringApplication app = new SpringApplication(App.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
    }
}
-5
votes

The issue seems to be with your yml file

tasks:231232 

should be

tasks: 231232