[英]java spring context:property-placeholder - set properties paths from placeholder
[英]spring: access properties set from <context:property-placeholder>
我正在使用设置属性:
<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集吗?
环境无法访问从属性文件设置的属性,它只能从系统和环境属性读取属性。
不,您不能访问property-placeholder
内部使用的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文件中,也可以在组件扫描时检测到它。 两种方法都应该起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.