Home > other >  How do I read config values from application.yml in Spring 2.7.2
How do I read config values from application.yml in Spring 2.7.2

Time:07-12

I've created a simple repo here that demonstrates my issue, just in case it helps anyone: Debugger showing environment


I changed FeedsConfig to this and it's working! Thank you Nicholas!

@ConstructorBinding
@ConfigurationProperties(prefix = "feeds")
public class FeedsConfig {
    public FeedsConfig(List<String> urls, String abc) {
        this.urls = urls;
        this.abc = abc;
    }
    public List<String> urls;
    public String abc;
}

CodePudding user response:

Assuming the components are properly scanned, I see two issues:

  • FeedsConfig should not be annotated with @Component
  • FeedsConfig should have a full-args constructor and be annotated with @ConstructorBinding to wire the properties

This can be easily achieved using Lombok (I use the same set-up on my machine):

@Data
@ConfigurationProperties(prefix = "feeds")
@ConstructorBinding
@RequiredArgsConstructor
public class FeedsConfig {

    public List<String> urls;
    public String abc;
}

CodePudding user response:

Looks like your configuration properties class is missing @ConfigurationPropertiesScan annotation. Also @Component and @EnableConfigurationProperties is no longer needed. Check out this tutorial, paragraph 3.1 https://www.baeldung.com/configuration-properties-in-spring-boot

@ConfigurationProperties(prefix = "feeds")
@EnableConfigurationProperties
public class FeedsConfig {
    public List<String> urls;
    public String abc;
}
  • Related