Iwavenice
Iwavenice

Reputation: 584

Springboot @ConfigurationProperties nested yaml properties don't load

For some reason non-nested properties load but nested don't.

Configuration:

spring:
  profile: junit
  profiles:
    include: base

Config class:

@ConfigurationProperties(prefix = "spring")
public class MyFirstProperties {

    private String profile;
    private Profiles profiles;
    // getters and setters


    public class Profiles
    {
        private String include;
    // getters and setters
    }
}

Main class:

    @SpringBootApplication
    @EnableConfigurationProperties(MyFirstProperties.class)
    public class Main {
        public static void main(String... args) {
            SpringApplication.run(Main.class, args);
        }
}

When I inject configuration class to my controller and call getter for a non-nested property it returns its value. But a getter for a nested property returns null.

Annotating inner class with ConfigurationProperties and its own prefix does not seem to work. Am I missing something?

Upvotes: 5

Views: 6888

Answers (1)

LppEdd
LppEdd

Reputation: 21104

You need to instantiate your profiles property

private Profiles profiles = new Profiles();

That's it.

This happens because your inner class isn't static.
You cannot instantiate this type of class directly, but only inside the context of the enclosing one.

Make your class static and you'll be good to go

public static class Profiles { ... }

Upvotes: 6

Related Questions