Search code examples
javaspring-bootconfiguration

Spring Boot. Why ConfigurationProperties prefix ignored if @Value used


I have:

inner:
  system:
    number: "01"
    id: "ID"
@Configuration
@ConfigurationProperties(prefix = "inner.system")
@Slf4j(topic = "Configuration Properties Logger")
@Data
public class ConfigurationProperties {
  private String number;
  @Value("${inner.system.id})
  private String id;
}

and everything loaded correctly.

But if I change @Value("${inner.system.id}) to @Value("${id}) (because of specified prefix "inner.system") it fails. I really confused with this.

Question

Is this the correct behavior or is there something incorrectly configured in my project?


Solution

  • It looks like there is a small mistake in the configuration. It should be,

    @Configuration
    @ConfigurationProperties(prefix = "inner.system")
    ....
    

    The @Value annotation requires the full property path, including the prefix. In this scenario, the full property path is inner.system.id In your case, the correct annotation would be @Value("${inner.system.id}") because the full path is "inner.system.id."