'Spring Boot - Nested Configuration Properties are not recnognized

As the title says, my custom properties are not registered upon application's start. Would you mind taking a look?

My custom.yml:

bridge:
  url: xxx
  authentication:
    auth-url: xxx
    user: xxx

My BridgeProperties.java:

@Component
@PropertySource("classpath:custom.yml")
@ConfigurationProperties(
        prefix = "bridge"
)

public class BridgeProperties {
    @NestedConfigurationProperty
    protected ApiProperties apiProperties = new ApiProperties();
}

My ApiProperties.java:

  public class ApiProperties {
    protected String url;
    protected ApiProperties.Authentication authentication;
    // getter and setter

    public ApiProperties() {
    }

    public static class Authentication {
      protected String authUrl;
      protected String user;
      public Authentication() {}
      // getter and setter
    }

My Application's entry point:

@SpringBootApplication
@PropertySources(value = {
        @PropertySource("classpath:application.yml"),
        @PropertySource("classpath:custom.yml")
})

public class IntegrationService {
    public static void main(String... args) {
        SpringApplication.run(IntegrationService.class, args);
    }
}

When printing to the command line, I get null instead of the values I assigned to url, auth-url, and user in the custom.yml.



Solution 1:[1]

The issue is that your class models do not match the YAML. Additionally, @NestedConfigurationProperty is only used by the metadata generator (to indicate that a property is not a single value but something we should explore to generate additional metadata). The following should work:

@Configuration
@PropertySource("classpath:custom.yml")
@ConfigurationProperties(
        prefix = "bridge"
)
public class BridgeProperties {
    protected String url;
    protected BridgeProperties.Authentication authentication;
    
    // getter and setter

    public BridgeProperties(){
    }

    public static class Authentication {
      protected String authUrl;
      protected String user;
      public Authentication() {}
      
      // getter and setter
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1