V
V
Victor Marquardt2019-02-23 14:12:31
Java
Victor Marquardt, 2019-02-23 14:12:31

Why isn't Spring Boot loading application.yml?

Spring boot does not load application.yml.
Tried to use @Value, @ConfigurationProperties, @EnableConfigurationProperties, @PropertySource("classpath:src/main/resources/application.yml"), snakeyaml library - result test = null
5c712a604cf14321902408.png
Please tell me where is the error.

// Application.java
package com.my.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = "com.my")
public class Application
{
    public static void main(String[] args)
    {
        SpringApplication.run(Application.class, args);
    }
}

// TestClass.java
package com.my.application;

public class TestClass
{
    public TestClass()
    {

    }
}

//ApplicationConfiguration
package com.my.configuration;

import com.my.application.TestClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class ApplicationConfiguration
{
    @Autowired
    Environment env;

    @Bean TestClass getTestClass()
    {
        System.out.println(env.getProperty("test"));

        return new TestClass();
    }
}

// pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.my</groupId>
    <artifactId>test</artifactId>
    <version>1.3.0</version>
    <packaging>pom</packaging>
    <name>test</name>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.23</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

// src/main/resources/application.yml

test: 123

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan, 2019-02-23
@Cempl

If you are working in Intellij IDEA, then check in the project settings if this file is added correctly.
I have a completely different, but similar situation. When I created a file in intellij IDEA and selected yml || properties, it turns out that it did not automatically add the required format to the file name. So I got files called application-dev instead of application-dev.properties

D
Dmitry Osipov, 2019-02-25
@Shiftuia

If I'm not mistaken, then resources are added to the classpath.
Therefore, the option should work

@PropertySource(value = "classpath:application.yml")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question