Immutable @ConfigurationProperties

后端 未结 7 1902
误落风尘
误落风尘 2020-12-08 13:55

Is it possible to have immutable (final) fields with Spring Boot\'s @ConfigurationProperties annotation? Example below

@ConfigurationProperties(         


        
相关标签:
7条回答
  • 2020-12-08 14:22

    You can set the field values through @Value annotations. These can be placed directly on the fields and don't require any setters:

    @Component
    public final class MyProps {
    
      @Value("${example.neededProperty}")
      private final String neededProperty;
    
      public String getNeededProperty() { .. }
    }
    

    The downside of this approach is:

    • You'll need to specified the fully-qualified property name on each field.
    • Validation doesn't work (cf. this question)
    0 讨论(0)
提交回复
热议问题