pre*_*oid 1 java spring spring-mvc spring-boot
我导入一个具有一些带有@Value字段服务的依赖项。在我的Spring Boot应用程序中,我不使用这些服务,但仍然使用此依赖项中的其他一些类,现在,如果我运行应用程序,它将无法解析占位符而失败,例如
原因:java.lang.IllegalArgumentException:无法解析值“ $ {apn.authentication.token.teamId}”中的占位符“ apn.authentication.token.teamId”。
因此,要解决此问题,我必须在属性中定义值。我搜索了一个设置,以使我的应用程序不会因未知值而失败,但我找不到解决方法。
即使缺少值,是否可以让我的spring boot应用启动?还是应该排除不使用的类(如果这是唯一的选择,怎么办)?
您可以设置一些默认值,以便如果不存在该值,它将采用默认值
@Value("${apn.authentication.token.teamId: -99}")
private int teamId;
Run Code Online (Sandbox Code Playgroud)
或将值设置为null
@Value("${apn.authentication.token.teamId: #{null}}")
private Integer teamId;
Run Code Online (Sandbox Code Playgroud)
您可以将您的配置配置PropertySourcesPlaceholderConfigurer为不会因未知占位符而失败:
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
return propertySourcesPlaceholderConfigurer;
}
Run Code Online (Sandbox Code Playgroud)
它不会失败,也不会费心去解决它。一般来说,最好在未知属性上失败(它们是属性,因为您的应用程序需要它们才能运行),或者添加它们默认值。如果您的配置对于应用程序的运行并不重要,您可以创建一个额外的配置文件并在运行时读取它。
| 归档时间: |
|
| 查看次数: |
1053 次 |
| 最近记录: |