Magnus
Magnus

Reputation: 728

Spring loads a configuration class with only empty member variables

I have a Spring web application, and I'm trying to load a YAML configuration file into a Java configuration class. However, my configuration class only contains empty member variables after it is instantiated in my JUnit test. I'm new to Spring and have probably missed something obvious. I built the project with Maven and have a Maven style directory tree.

My configuration Java class:

src/main/java/com/my/package/config/YAMLConfigDatabase:

package com.my.package;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "database")

public class YAMLConfigDatabase {

    private String url;

    private int port;

    private String schema;

    private String username;

    private String password;

    //Getters and setters are all here.

}

My configuration YAML file:

src/main/resources/application.yml

server.port: 8090

database:
  url: 'localhost'
  port: 3306
  schema: 'my_schema'
  username: 'webappuser'
  password: 'secretPassword'

My JUnit test to check whether I can indeed load the configuration file:

package com.my.package;

import com.my.package.config.YAMLConfigDatabase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {YAMLConfigDatabase.class})
public class YAMLConfigTest {


    private YAMLConfigDatabase config;

    @Autowired
    public void setYAMLConfigDatabase(YAMLConfigDatabase config){
        this.config = config;
    }

    @Test
    public void isYAMLConfigLoaded(){
        System.out.println(this.config);
        System.out.println(this.config.getPassword()); 
        //The above line returns "null", but I would like it to return "secretPassword".
    }
}

EDIT:

I changed my YAMLConfigDatabase.java to look like this:

package com.my.package.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "database")
@PropertySource(value = "classpath:application.yml") //new line
@Component
public class YAMLConfigDatabase {

    @Value("${url}") //new line
    private String url;

    @Value("${port}") //new line
    private Integer port;

    @Value("${schema}") //new line
    private String schema;

    @Value("${username}") //new line
    private String username;

    @Value("${password}") //new line
    private String password;
}

I used Senior Promidor tip to add the @Value annotation to all member variables, and I also had to add the line @PropertySource(value = "classpath:application.yml"). If I skipped the latter step, the arguments inside the @Value annotations were interpreted literally, as I mentioned in a comment.

Upvotes: 0

Views: 2905

Answers (1)

mndeveci
mndeveci

Reputation: 301

Since I also fight with this a lot (@ConfigurationProperties), I tried to test your code with a simple application;

This is a Spring Boot Application with version 2.0.0 (I also tested it with 1.5.14)

application.yml:

custom:
  var1: aaa
  var2: bbb
  var3: ccc

ConfigClass.java

@Configuration
@ConfigurationProperties(prefix = "custom")
public class ConfigFile {

    private String var1;
    private String var2;
    private String var3;

    public String getVar1() {
        return var1;
    }

    public void setVar1(String var1) {
        this.var1 = var1;
    }

    public String getVar2() {
        return var2;
    }

    public void setVar2(String var2) {
        this.var2 = var2;
    }

    public String getVar3() {
        return var3;
    }

    public void setVar3(String var3) {
        this.var3 = var3;
    }

    @Override
    public String toString() {
        return "ConfigFile{" +
                "var1='" + var1 + '\'' +
                ", var2='" + var2 + '\'' +
                ", var3='" + var3 + '\'' +
                '}';
    }
}

and the test class:

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootAppTests {

    @Autowired
    ConfigFile configFile;

    @Test
    public void contextLoads() {
        Assert.assertEquals(configFile.getVar1(), "aaa");
        Assert.assertEquals(configFile.getVar2(), "bbb");
        Assert.assertEquals(configFile.getVar3(), "ccc");
    }

}

And at the end of test it succeeds all cases.

I am just guessing; maybe you should remove some extra annotations that you added and don't needed anymore.

Upvotes: 1

Related Questions