如何配置 Spring bean 容器(或应用程序上下文)来加载 Java 属性文件?
Java世界 http://www.javaworld.com/文章智能加载您的属性 http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html解释如何使用标准 Java 库中的以下资源处理方法之一从类路径加载属性文件:
ClassLoader.getResourceAsStream ("some/pkg/resource.properties");
Class.getResourceAsStream ("/some/pkg/resource.properties");
ResourceBundle.getBundle ("some.pkg.resource");
如何使用 Spring bean 容器执行相同的操作?
The Spring 框架参考文档 (2.5.x) http://static.springsource.org/spring/docs/2.5.x/reference/xsd-config.html#xsd-config-body-schemas-util-properties给出了两个如何将属性文件加载到 bean 容器中的示例,一个是在 2.5 版本发布之前,另一个是使用<util:properties/>
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"/>
请注意,为了使用<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)