java Spring 占位符不解析 JavaConfig 中的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29140794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring placeholder doesn't resolve properties in JavaConfig
提问by herau
Currently i have a Spring xml configuration (Spring 4) which load a properties file.
目前我有一个加载属性文件的 Spring xml 配置(Spring 4)。
context.properties
上下文属性
my.app.service = myService
my.app.other = ${my.app.service}/sample
Spring xml configuration
Spring xml 配置
<bean id="contextProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="ignoreResourceNotFound" value="true" />
<property name="fileEncoding" value="UTF-8" />
<property name="locations">
<list>
<value>classpath:context.properties</value>
</list>
</property>
</bean>
<bean id="placeholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="properties" ref="contextProperties" />
<property name="nullValue" value="@null" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
</bean>
Bean which use the properties
使用属性的Bean
@Component
public class MyComponent {
@Value("${my.app.other}")
private String others;
}
This works perfectly and the others
value is MyService/sample
, as excepted. But when i try to replace this configuration by a JavaConfig, the @Value
in my component doesn't works in the same way. the value isn't myService/sample
but ${my.app.service}/sample
.
这完美地工作并且others
值是MyService/sample
,作为例外。但是当我尝试用 JavaConfig 替换此配置时@Value
,我的组件中的工作方式不同。值不是myService/sample
但是${my.app.service}/sample
。
@Configuration
@PropertySource(name="contextProperties", ignoreResourceNotFound=true, value={"classpath:context.properties"})
public class PropertiesConfiguration {
@Bean
public static PropertyPlaceholderConfigurer placeholder() throws IOException {
PropertyPlaceholderConfigurer placeholder = new PropertyPlaceholderConfigurer();
placeholder.setNullValue("@null");
placeholder.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
return placeholder;
}
}
Did i miss something in the conversion from xml to Javaconfig ?
我在从 xml 到 Javaconfig 的转换中遗漏了什么吗?
ps : I also try to instantiate a PropertySourcesPlaceholderConfigurer
instead of a PropertyPlaceholderConfigurer
without more success.
ps:我也尝试实例化 aPropertySourcesPlaceholderConfigurer
而不是 aPropertyPlaceholderConfigurer
没有更多的成功。
采纳答案by Mithun
Update to use configure PropertySourcesPlaceholderConfigurer
. Just having @PropertySource
annotation will not be sufficient:
更新以使用 configure PropertySourcesPlaceholderConfigurer
。仅仅有@PropertySource
注释是不够的:
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
return new PropertySourcesPlaceholderConfigurer();
}
@PropertySource
annotation does not automatically register a PropertySourcesPlaceholderConfigurer
with Spring. Hence we need to explicitly configure PropertySourcesPlaceholderConfigurer
@PropertySource
注解不会自动PropertySourcesPlaceholderConfigurer
向 Spring注册 a 。因此我们需要显式配置PropertySourcesPlaceholderConfigurer
Below JIRA ticket has more information on the rationale behind this design:
下面 JIRA 票有更多关于这种设计背后的基本原理的信息:
https://jira.spring.io/browse/SPR-8539
https://jira.spring.io/browse/SPR-8539
UPDATE:Created simple Spring boot application to use nested properties. It is working fine with the above configuration.
更新:创建了简单的 Spring Boot 应用程序以使用嵌套属性。使用上述配置可以正常工作。
https://github.com/mgooty/property-configurer/tree/master/complete
https://github.com/mgooty/property-configurer/tree/master/complete
回答by Alfredo Diaz
Another option is to import PropertyPlaceholderAutoConfiguration.class.
另一种选择是导入 PropertyPlaceholderAutoConfiguration.class。
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
@Import(PropertyPlaceholderAutoConfiguration.class)
The annotation includes a PropertySourcesPlaceholderConfigurer in the context if it doesn't exist.
如果不存在,则注释在上下文中包含一个 PropertySourcesPlaceholderConfigurer。