Spring-boot: set default value to configurable properties

别说谁变了你拦得住时间么 提交于 2019-12-03 22:27:49

Check if property1 was set using a @PostContruct in your MyProperties class. If it wasn't you can assign it to another property.

@PostConstruct
    public void init() {
        if(property1==null) {
            property1 = //whatever you want
        }
    }

In spring-boot 1.5.10 (and possibly earlier) setting a default value works as-per your suggested way. Example:

@Component
@ConfigurationProperties(prefix = "myprefix")
public class MyProperties {

  @Value("${spring.application.name}")
  protected String appName;
}

The @Value default is only used if not overridden in your own property file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!