如何配置Springbean容器来加载Java属性文件?
如何配置Springbean容器(或应用程序上下文)来加载Java属性文件 本文介绍如何在标准Java库中使用以下资源处理方法之一从类路径加载属性文件:如何配置Springbean容器来加载Java属性文件?,java,spring,properties,classpath,Java,Spring,Properties,Classpath,如何配置Springbean容器(或应用程序上下文)来加载Java属性文件 本文介绍如何在标准Java库中使用以下资源处理方法之一从类路径加载属性文件: ClassLoader.getResourceAsStream ("some/pkg/resource.properties"); Class.getResourceAsStream ("/some/pkg/resource.properties"); ResourceBundle.getBundle ("some.pkg.resource")
ClassLoader.getResourceAsStream ("some/pkg/resource.properties");
Class.getResourceAsStream ("/some/pkg/resource.properties");
ResourceBundle.getBundle ("some.pkg.resource");
如何使用SpringBean容器实现同样的功能?您的
beans.xml
文件应具有属性PlaceHolderConfigure
:
classpath:some/pkg/resource.properties
价值
然后您可以在beans.xml
中的其他地方引用这些属性:
....
有关这方面的文章,请查看我们使用的:
<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>
类路径:test.properties
它允许在那里定义的属性用作配置文件中的引用
有关更多信息,请参阅:
有一个叫做PropertyPlaceHolderConfigure的东西,您可以使用它将属性文件中的值注入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>
类路径:com/foo/jdbc.properties
例如,通过。使用通过属性在上下文中配置bean
您将在其他答案中找到PropertyPlaceHolderConfigure示例。以下是PropertiesFactoryBean示例:
<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value=classpath:config/applicationConfig.properties"/>
</bean>
如果要将对象引用为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>
真的
类路径:property-file.properties
这允许您引用Springbeanproperties
作为java.util.properties
的实例。您甚至可以通过向位置添加更多值来将多个属性文件合并在一起。有关Spring将接受哪些类型的位置值的信息,请参见的此说明。如果您想在SpringXML中使用${}
样式替换,您可以看到有许多其他答案描述了如何做到这一点。给出了两个如何将属性文件加载到bean容器中的示例,在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>
<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<util:properties id="jdbcConfiguration" location="classpath:com/foo/jdbc-production.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>
<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<util:properties id="jdbcConfiguration" location="classpath:com/foo/jdbc-production.properties"/>
查看附录A中的模式和文档,显然没有。您的答案是唯一同时提到PropertiesFactoryBean
和PropertyPlaceHolderConfigure
的答案。您是否愿意详细介绍这两个类之间的区别?如果您想将属性实例作为bean注入,那么请使用PropertiesFactoryBean。如果要基于属性文件的值参数化其他bean,请使用PropertyPlaceHolderConfigure。我相信PropertiesFactoryBean就是您在这里需要的。这是一个微妙但非常重要的区别。是的,它们有不同的用途。与我在问题中提到的方法不同,function
和classPropertiesFactoryBean
,PropertyPlaceHolderConfigure
方法不会使属性在上下文之外可见。与我在问题中提到的方法不同,函数
和类属性FactoryBean
,PropertyPlaceHolderConfigure
方法不会使属性在上下文之外可见。此解决方案更接近我在问题中提到的方法,其行为类似于函数
。与我在问题中提到的方法不同,函数
,而classPropertiesFactoryBean
,PropertyPlaceholderConfigurer
方法不会使属性在上下文之外可见。