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

时间:2010-06-04 15:14:49

标签: java spring properties classpath

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

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

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

如何使用Spring bean容器执行相同的操作?

6 个答案:

答案 0 :(得分:16)

Spring Framework Reference Documentation (2.5.x)提供了两个如何将属性文件加载到bean容器中的示例,一个在版本2.5发布之前,另一个使用2.5版中引入的<util:properties/>函数的简洁方法:

在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>

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"/>

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

<?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>

答案 1 :(得分:7)

您的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>

然后您可以在beans.xml

的其他地方引用这些属性
<bean class="${blah}">
    ....
<bean>

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

答案 2 :(得分:5)

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

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

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

答案 3 :(得分:2)

有一个名为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>

答案 4 :(得分:1)

我们用这个:

<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>

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

有关详细信息,请参阅:

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

答案 5 :(得分:1)

如果要将对象引用为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>

这允许您将Spring bean properties作为java.util.Properties的实例引用。您甚至可以通过向location添加更多值将多个属性文件合并在一起。有关Spring将接受的位置值的类型的信息,请参阅resource strings的此描述。如果您想在Spring XML中使用${}样式替换,您可以看到有许多其他答案描述如何执行此操作。