Home > database >  Spring Boot - Nested Configuration Properties are not recnognized
Spring Boot - Nested Configuration Properties are not recnognized

Time:02-11

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.

Anyone an idea? Thank you very much in advance!

CodePudding user response:

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

CodePudding user response:

Tank you for your answer! However, it does not work with the snipped you posted, I guess you mixed up with the class names. However, you guided me into the right direction. At least I am getting a value assigned to the url with the following code for BridgeProperties.java:

@Component
@PropertySource("classpath:custom.yml")
@ConfigurationProperties(
        prefix = "bridge"
)
public class BridgeProperties {
    @Value("${url}")
    protected String url;
    protected BridgeProperties.Authentication authentication;

    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }

    public BridgeProperties.Authentication getAuthentication() {
        return authentication;
    }
    public void setAuthentication(BridgeProperties.Authentication authentication) {
        this.authentication = authentication;
    }

    public BridgeProperties(){

    }

    public static class Authentication {
        @Value("${auth-url}")
        protected String authUrl;
        @Value("${user}")
        protected String user;
        @Value("${key}")
        protected String key;

        public Authentication() {
        }

        public String getAuthUrl() {
            return this.authUrl;
        }
        public void setAuthUrl(String authUrl) {
            this.authUrl = authUrl;
        }

        public String getUser() {
            return this.user;
        }
        public void setUser(String user) {
            this.user = user;
        }

        public String getKey() {
            return this.key;
        }
        public void setKey(String key) {
            this.key = key;
        }

    }

}

However, what does not work is the part for the authentication. I still receive null values. Any further ideas?

Edited, but still not working:

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

    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }

    public BridgeProperties.Authentication getAuthentication() {
        return authentication;
    }
    public void setAuthentication(BridgeProperties.Authentication authentication) {
        this.authentication = authentication;
    }

    public BridgeProperties(){

    }

    public static class Authentication {
        protected String authUrl;
        protected String user;
        protected String key;

        public Authentication() {
        }

        public String getAuthUrl() {
            return this.authUrl;
        }
        public void setAuthUrl(String authUrl) {
            this.authUrl = authUrl;
        }

        public String getUser() {
            return this.user;
        }
        public void setUser(String user) {
            this.user = user;
        }

        public String getKey() {
            return this.key;
        }
        public void setKey(String key) {
            this.key = key;
        }

    }

}

With updates main class, still not working:

@SpringBootApplication
@PropertySources(value = {
        @PropertySource("classpath:application.yml"),
        @PropertySource("classpath:custom.yml")
})
@EnableConfigurationProperties(BridgeProperties.class)
public class IntegrationService {
    public static void main(String... args) {
        SpringApplication.run(IntegrationService.class, args);
    }
}
  • Related