[英]How can I set Spring property from the XML configuration file?
I have some spring config that uses a property, like so: 我有一些使用属性的spring配置,如下所示:
<bean id="foo" class="...">
<constructor-arg value="${aProperty}"/>
</bean>
Obviously I know I can resolve this property by having a properties file (say example.properties): 显然我知道我可以通过一个属性文件(例如example.properties)来解析这个属性:
aProperty=value
and importing this file in the Spring config: 并在Spring配置中导入此文件:
<bean id="propertyConfiguration" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>example.properties</value>
</list>
</property>
</bean>
My question is, can I set this property directly in the XML file instead of having to create a separate properties file? 我的问题是,我可以直接在XML文件中设置此属性,而不必创建单独的属性文件吗? Something like this would be ideal: 这样的事情是理想的:
<set-property name="aProperty" value="value"/>
Maven has a similar feature for pom files: Maven对pom文件有类似的功能:
<properties><aProperty>value</aProperty></properies>
The goal of using a properties file is uncouple values from Spring configuration files, so it's a little weird define a property in the same configuration file. 使用属性文件的目标是从Spring配置文件中取消值,因此在同一配置文件中定义属性有点奇怪。 Nevertheless you always can add properties to your PropertyPlaceholderConfigurer: 不过,您始终可以向PropertyPlaceholderConfigurer添加属性:
<bean id="propertyConfiguration" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>example.properties</value>
</list>
</property>
<property name="properties">
<props>
<prop key="aa">bb</prop>
<prop key="cc">dd</prop>
</props>
</property>
</bean>
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.