【发布时间】:2019-09-25 09:49:47
【问题描述】:
Spring 不会修剪属性文件中给出的值。根据这里的讨论,看起来他们是故意留下来的。 但是,在我们的项目中,我们希望在应用程序使用之前自动修剪这些值。
我正在使用 2.1.4.RELEASE。
我尝试添加以下 Bean 配置
@Bean
public static PropertyPlaceholderConfigurer properties() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
Resource[] resources = new ClassPathResource[]{new ClassPathResource("application.properties")};
ppc.setLocations(resources);
ppc.setIgnoreUnresolvablePlaceholders(true);
ppc.setTrimValues(true);
return ppc;
}
由于此设置,它无法加载属性文件并引发以下异常
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'kafka.groupId' in value "${kafka.groupId}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:237)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:211)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:175)
有人试过解决这个问题吗?
我参考了以下链接,但没有得到太多帮助。
Automatically Trim Trailing White Space for properties in Props file loaded into Spring,
https://htr3n.github.io/2018/11/spring-boot-trailing-whitespaces/
【问题讨论】:
-
你的 spring boot 版本是多少?
-
@clevertension 2.1.4.RELEASE
标签: java spring-boot properties-file