我正在使用以下命令设置属性:
<context:property-placeholder location="#{ T(System).getenv().get('DEV_PROPERTIES') ?: 'classpath:/META-INF/properties/config.properties' }"/>我可以访问以下属性:
@Value("${hostname}")
String hostname;`这可以很好地工作。
但是,我想使用属性映射来访问属性,或者只是简单地在不能使用@Value变量的方法中获取值。是否有一种方法可以使用<context:property-placeholder />注入属性bean集?
Environment没有访问属性文件中设置的属性的权限,它只能从系统和环境属性中读取属性。
发布于 2015-01-25 16:20:37
不,您不能访问property-placeholder内部使用的属性。您可以做的是将属性加载到Properties对象中,并将其注入到property-placeholder中,也可以将其注入到您喜欢的任何位置。
另一个技巧是你不需要使用SpEL来实现你想要的location属性,一个简单的占位符就可以完成这个任务。
要加载属性对象,请使用util名称空间。
<util:properties id="props" location="${DEV_PROPERTIES:classpath:/META-INF/properties/config.properties}" />
<context:property-placeholder properties-ref="props" />要使您想要的属性对Environment可用,您应该在@Configuration类上使用@PropertySource注释。
@Configuration
@PropertySource("${DEV_PROPERTIES:classpath:/META-INF/properties/config.properties}")
public class ApplicationConfig { ... }您可以将其作为bean添加到xml文件中,也可以在扫描组件时对其进行检测。无论哪种方式都应该有效。
https://stackoverflow.com/questions/28132533
复制相似问题