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

发布于 2024-09-04 05:38:19 字数 527 浏览 4 评论 0原文

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

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

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

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

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

JavaWorld article Smartly Load Your Properties explains how to load property files from the classpath using one of the following resource processing methods in the standard Java library:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

雨巷深深 2024-09-11 05:38:19

Spring 框架参考文档 (2.5.x) 提供了两个如何将属性文件加载到 Bean 容器中的示例,一个是在版本 2.5 发布之前,另一个是使用 函数在 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>

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

请注意,为了使用 ,您必须声明 < Spring XML 配置文件顶部序言中的 code>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>

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:

Before version 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:

<!-- 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 util namespace and schema location in the preamble at the top of your Spring XML configuration file:

<?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>
无可置疑 2024-09-11 05:38:19

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

Your beans.xml file should have a 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:

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

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

胡大本事 2024-09-11 05:38:19

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

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

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

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

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

<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location" value=classpath:config/applicationConfig.properties"/>
</bean>
睫毛溺水了 2024-09-11 05:38:19

有一个叫做 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>

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

<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>
悲欢浪云 2024-09-11 05:38:19

我们使用这个:

<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

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

猥︴琐丶欲为 2024-09-11 05:38:19

如果您想将该对象作为 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 将接受哪些类型的值作为位置信息。如果您想在 Spring XML 中使用 ${} 样式替换,您可以看到有许多其他答案描述了如何做到这一点。

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

<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 properties as 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 strings for 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文