如何配置 Spring bean 容器来加载 Java 属性文件?

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

How do you configure a Spring bean container to load a Java property file?

javaspringpropertiesclasspath

提问by Derek Mahar

How do you configure a Spring bean container (or application context) to load a Java property file?

如何配置 Spring bean 容器(或应用程序上下文)来加载 Java 属性文件?

JavaWorldarticle Smartly Load Your Propertiesexplains how to load property files from the classpath using one of the following resource processing methods in the standard Java library:

JavaWorld文章Smartly Load Your Properties解释了如何使用标准 Java 库中的以下资源处理方法之一从类路径加载属性文件:

ClassLoader.getResourceAsStream ("some/pkg/resource.properties");
Class.getResourceAsStream ("/some/pkg/resource.properties");
ResourceBundle.getBundle ("some.pkg.resource");

How can you do the same using a Spring bean container?

如何使用 Spring bean 容器做同样的事情?

采纳答案by Derek Mahar

The Spring Framework Reference Documentation (2.5.x)gives two examples of how to load a property file into a bean container, one before the release of version 2.5 and a more concise way using the <util:properties/>function that was introduced in version 2.5:

Spring框架参考文档(2.5.X)给出了如何将属性文件加载到一个bean的容器,一个2.5版本的发布和使用更简洁的方式前两个例子<util:properties/>功能是在2.5版本中引入的:

Before version 2.5:

2.5 版之前:

<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<bean id="jdbcConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="location" value="classpath:com/foo/jdbc-production.properties"/>
</bean>

After version 2.5:

2.5 版之后:

<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<util:properties id="jdbcConfiguration" location="classpath:com/foo/jdbc-production.properties"/>

Note that in order to use <util:properties/>, you must declare the utilnamespace and schema location in the preamble at the top of your Spring XML configuration file:

请注意,为了使用<util:properties/>,您必须util在 Spring XML 配置文件顶部的序言中声明命名空间和模式位置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">

<!-- <bean/> definitions here -->

</beans>

回答by Edward Dale

Your beans.xmlfile should have a PropertyPlaceholderConfigurer:

您的beans.xml文件应该有PropertyPlaceholderConfigurer

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:some/pkg/resource.properties</value>
        </list>
    </property>
    <!-- Default values for backwards compatibility -->
    <property name="properties">
        <props>
            <prop key="name">value</prop>
        </props>
    </property>
</bean>

And then you can reference the properties as such elsewhere in the beans.xml:

然后您可以在以下其他位置引用这些属性beans.xml

<bean class="${blah}">
    ....
<bean>

For an article about this, check out http://almaer.com/blog/spring-propertyplaceholderconfigurer-a-nice-clean-way-to-share

有关这方面的文章,请查看http://almaer.com/blog/spring-propertyplaceholderconfigurer-a-nice-clean-way-to-share

回答by Peter Tillemans

We use this :

我们使用这个:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="locations">
        <value>classpath:test.properties</value>
    </property>
</bean>

Which allows the properties defined there to be used as references in the config files

这允许在那里定义的属性用作配置文件中的引用

For more info see :

有关更多信息,请参阅:

http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html

http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html

回答by Nathan Hughes

There's this thing called a PropertyPlaceholderConfigurer, you can use it to inject your beans with values from a properties file, like this:

有一个叫做 PropertyPlaceholderConfigurer 的东西,你可以用它从属性文件中注入你的 bean,像这样:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:com/foo/jdbc.properties</value>
    </property>
</bean>

<bean id="dataSource" destroy-method="close"
      class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

回答by Timo Westk?mper

For example via the PropertiesFactoryBean. Use the PropertyPlaceholderConfigurerto configure beans in the context via properties.

例如通过PropertiesFactoryBean。使用PropertyPlaceholderConfigurer通过属性在上下文中配置 bean。

You will find PropertyPlaceholderConfigurer examples in the other answers. Here is a PropertiesFactoryBean example :

您将在其他答案中找到 PropertyPlaceholderConfigurer 示例。这是一个 PropertiesFactoryBean 示例:

<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location" value=classpath:config/applicationConfig.properties"/>
</bean>

回答by laz

If you want to reference the object as an instance of java.util.Properties, you should do the following:

如果要将对象引用为 的实例java.util.Properties,则应执行以下操作:

<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="ignoreResourceNotFound"><value>true</value></property>
    <property name="locations">
        <list>
            <value>classpath:property-file.properties</value>
        </list>
    </property>
</bean>

This allows you to reference the Spring bean propertiesas an instance of java.util.Properties. You can even have it merge together multiple properties files by adding more values to location. See this description of resource stringsfor information about what types of values Spring will accept for location. If you are wanting to use ${}style substitutions in the Spring XML, you can see there are a number of other answers describing how to do that.

这允许您将 Spring beanproperties作为java.util.Properties. 您甚至可以通过将更多值添加到location. 有关Spring 将接受的位置值类型的信息,请参阅资源字符串的此描述。如果您想${}在 Spring XML 中使用样式替换,您可以看到有许多其他答案描述了如何做到这一点。