Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot doesn't load application.yml config or?

Tags:

spring-boot

I have a simple main app:

// 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);
    }
}

Just a class:

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

public class TestClass
{
    public TestClass()
    {

    }
}

With config:

//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();
    }
}

this is my pom file:

// 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>

the application.yml file is just:

// src/main/resources/application.yml

test: 123

Property "test" always = null. What I was wrong? Tried with @Value, @ConfigurationProperties, @EnableConfigurationProperties, @PropertySource("classpath:src/main/resources/application.yml") annotations, without the snakeyaml library, with another spring-boot versions, but result always the same.

like image 747
Viktor Markvardt Avatar asked Mar 05 '23 11:03

Viktor Markvardt


2 Answers

It has to do with Maven - application.properties is not part of the build when you're using <packaging>pom</packaging> in your pom file - hence when you start the Application, the file is not there to be read.

Remove <packaging>pom</packaging> from your pom and you should be good to go.

like image 90
hovanessyan Avatar answered May 24 '23 14:05

hovanessyan


try adding dependency org.springframework.boot:spring-boot-configuration-processor

like image 44
Alex Avatar answered May 24 '23 13:05

Alex