我正在将一个小的“遗留”应用程序迁移到Spring,并在与YamlPropertiesFactoryBean结合使用属性占位符时遇到了问题。应用程序使用Spring配置。
我在项目中添加了一个Spring "main“,如下所示:
@SpringBootApplication
@ImportResource("classpath:spring-config.xml")
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}我已经把application.yaml放进/config了。它是由Spring获取的,我可以通过导入的Springconfig.xml中的bean声明中的属性替换(${some.prop})访问其中定义的属性。例如。
<bean id="myRouteBuilder" class="org.camelSpringBoot.test.MyRouteBuilder">
<property name="someProp" value="${prop.from.application.yaml}"/>
</bean>(除外)在使用org.springframework.beans.factory.config.YamlPropertiesFactoryBean读取额外的外部配置文件时除外:org.springframework.core.io.FileSystemResource构造函数arg中文件位置的属性无法解析:
<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources">
<list>
<bean class="org.springframework.core.io.FileSystemResource">
<constructor-arg value="${service.config.loc}" />
</bean>
</list>
</property>
</bean>收益率
java.io.FileNotFoundException:${service.config.loc}
如果我硬编码外部配置的位置,那么在外部YAML文件中使用的属性也不会被解析。
application.yaml:
security:
keystore:
loc: /security/my.keystore外部配置:
services:
a:
ssl:
secret: ${security.keystore.loc}java.lang.RuntimeException:无法打开密钥库/信任库${security.keystore.loc}
请注意,错误是关于逐字属性的。
与其他成功使用属性占位符的bean一样,YamlPropertiesFactoryBean实例是在spring-config.xml中声明的(意味着属性被它们的值替换)。为什么YamlPropertiesFactoryBean会失败?
是否可以为外部YAML文档启用占位符解析?
发布于 2018-12-07 13:25:25
我不知道你想做的事情是否奏效。属性解析是在完成BeanDefinition之后进行的,因此只能使用Spring,而不是像yaml文件那样的任意文件。
大多数构建工具(gradle,maven)都包含在构建时解析这些占位符的功能。那会不会是你的解决方案?
https://stackoverflow.com/questions/53670385
复制相似问题