2 回答

TA貢獻1875條經(jīng)驗 獲得超5個贊
您可以設(shè)置一些默認值,以便如果該值不存在,則采用默認值
@Value("${apn.authentication.token.teamId:-99}") private int teamId;
或?qū)⒅翟O(shè)置為空
@Value("${apn.authentication.token.teamId:#{null}}") private Integer teamId;

TA貢獻1900條經(jīng)驗 獲得超5個贊
您可以將您的配置配置PropertySourcesPlaceholderConfigurer為不會在未知占位符上失?。?/p>
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
return propertySourcesPlaceholderConfigurer;
}
它不會失敗,也不會費心去解決它。通常,在未知屬性(它們是屬性,因為您的應(yīng)用程序需要它們運行)或添加它們的默認值時失敗是一種很好的做法。如果您的配置對您的應(yīng)用程序運行不重要,您可以創(chuàng)建一個額外的配置文件并在運行時讀取它。
添加回答
舉報