简体   繁体   English

如何从XML配置文件中设置Spring属性?

[英]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.

相关问题 如何在xml配置文件中以编程方式设置属性? - How to set programmatically property in xml configuration file? 如何在Spring XML元数据配置中为bean设置ServletContext属性 - How to set ServletContext property for a bean in Spring XML metadata configuration 在春季如何将xml bean配置文件导入到@configuration类? - How can I import a xml bean configuration file to a @configuration class in spring? 使用来自其他bean属性的xml配置设置Spring bean属性 - Spring bean property set using xml configuration from other bean property Spring无法加载XML配置文件 - Spring can't load xml configuration file 用 xml 中的配置文件中的属性替换 uri - Replace uri with property from configuration file in xml 我们可以在spring XML配置文件中使用apache camel设置Kafka属性 - Can we set Kafka properties with apache camel, in spring XML configuration file Ant:我可以从xml文件中获取特定属性吗? - Ant: Can I get a specific property from a xml file? 如何保存用Apache Commons Configuration中的属性文件中的值填充的xml配置无法保存 - How to save xml configuration filled with values from properties file in Apache Commons Configuration can't save Spring Batch CommandLineJobRunner找不到.xml配置文件 - Spring Batch CommandLineJobRunner can't find .xml configuration file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM