【问题标题】:Default values for missing maven properties when expanded in spring application.properties在 spring application.properties 中展开时缺少 maven 属性的默认值
【发布时间】:2020-08-01 08:50:05
【问题描述】:

如果在application.properties 中定义的属性@property.absent@ 不能扩展(在pom.xml 中没有定义),则绑定值是文字@property.absent@。有没有一种简单的方法可以用自定义默认值(例如 null)覆盖它,尤其是。与@ConfigurationProperties一起自动配置?

【问题讨论】:

    标签: spring maven properties pom.xml configurationproperties


    【解决方案1】:

    application.yml 文件:

    test:
     property: @property.absent@
    

    配置属性类:

    @ConfigurationProperties("test")
    public class TestConfigurationProperties {
    
    private String property = "your default value";
    
    //Getters and Setters
    
    }
    

    【讨论】:

    • 是的,已经尝试过了,但不知何故没有奏效。具体来说,这里有一个精简的项目来说明这一点:expansion
    • 你没有使用'@Value'注解为了你的目的,请在'@Configuration'属性中使用常量值,比如private String missing = "Hello";
    • 是的,没有在ConfigurationProperties 中添加@Value,因为它不起作用。理想情况下,我想将复杂的嵌套配置属性映射到源(使用 ConfigurationProperties)与默认值结合起来。
    • 如果出现这种情况,请尝试分配默认值,例如 private String a = "default"
    • 是的,我也试过了,如果 application.properties 中根本没有该属性,这将起作用。但是在这种情况下,它仍然默认为@absent@
    猜你喜欢
    • 2021-09-20
    • 2015-06-19
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 2011-04-19
    • 2014-01-04
    • 2020-05-03
    相关资源
    最近更新 更多