在 aplicationContext.xml Spring 文件中存储自定义属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/888771/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:52:23  来源:igfitidea点击:

Store Custom properties in aplicationContext.xml Spring file

springproperties

提问by HyLian

I need to store some configuration parameters for a web application that uses spring framework.

我需要为使用 spring 框架的 Web 应用程序存储一些配置参数。

Typically I'll use a configurationfile.properties file but i wonder if i can store that values in the applicationContext.xml file.

通常我会使用 configurationfile.properties 文件,但我想知道是否可以将这些值存储在 applicationContext.xml 文件中。

One workaround could be to create a JavaBean class to store the values, and build that class using spring, something like this:

一种解决方法是创建一个 JavaBean 类来存储值,并使用 spring 构建该类,如下所示:

<bean id="configurationBean" class="mypackage.someClass">
 <property name="confValue1">
   <value>myValue1</value>
 </property>
 ....
</bean>

But i would like to know if is there a way to store those parameters without the needing to create that class.

但我想知道是否有办法存储这些参数而无需创建该类。

Thanks in advance.

提前致谢。



I think that the best solution that fits my requirements is to use a java.util.Properties instance as a Spring Bean.

我认为符合我要求的最佳解决方案是使用 java.util.Properties 实例作为 Spring Bean。

Thank you all.

谢谢你们。

回答by Darren Greaves

This should work with the following syntax.

这应该使用以下语法。

<bean id="props" class="java.util.Properties" >
    <constructor-arg>
        <props>
            <prop key="myKey">myValue</prop>
            <prop ...>
        </props>
    </constructor-arg>
</bean>

You are taking advantage of the fact that java.util.Properties has a copy constructor that takes a Properties object.

您正在利用 java.util.Properties 具有采用 Properties 对象的复制构造函数这一事实。

I do this for a HashSet which also has a copy constructor (as do HashMaps and ArrayLists) and it works perfectly.

我为 HashSet 执行此操作,它也有一个复制构造函数(与 HashMaps 和 ArrayLists 一样)并且它完美地工作。

回答by Mark

Spring has builtin support for specifying properties within the application context XML. See section 3.3.2.4of the Spring Reference docs.

Spring 内置支持在应用程序上下文 XML 中指定属性。请参阅Spring 参考文档的第 3.3.2.4 节

回答by cliff.meyers

I think you'll get the best results using Spring's PropertyPlaceholderConfigurer which allows you to map values from a regular .properties file against properties defined on your beans.

我认为使用 Spring 的 PropertyPlaceholderConfigurer 会获得最佳结果,它允许您将常规 .properties 文件中的值映射到 bean 上定义的属性。

http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-factory-placeholderconfigurer

http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-factory-placeholderconfigurer

The example shows how to set the JDBC connection properties directly on an instance of javax.sql.DataSource, eliminating the need for an intermediate "configuration bean."

该示例显示了如何直接在 javax.sql.DataSource 的实例上设置 JDBC 连接属性,从而无需中间的“配置 bean”。

回答by hank wall

The best way is to use spring PropertyPlaceholderConfigurer

最好的方法是使用 spring PropertyPlaceholderConfigurer

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:yourconfigurationfile.properties</value>
        </list>
    </property>
</bean>

then

然后

<bean id="configurationBean" class="mypackage.someClass">
    <property name="confValue1">
        <value>${myvalue1}</value>
    </property>
    ....
</bean>  

and in yourconfigurationfile.properties

并在 yourconfigurationfile.properties 中

myvalue1= value1