如何配置 Spring bean 容器来加载 Java 属性文件?
如何配置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Spring 框架参考文档 (2.5.x) 提供了两个如何将属性文件加载到 Bean 容器中的示例,一个是在版本 2.5 发布之前,另一个是使用
函数在 2.5 版本中引入:2.5 版本之前:
2.5 版本之后:
请注意,为了使用
,您必须声明 < Spring XML 配置文件顶部序言中的 code>util 命名空间和架构位置: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:
After version 2.5:
Note that in order to use
<util:properties/>
, you must declare theutil
namespace and schema location in the preamble at the top of your Spring XML configuration file:您的
beans.xml
文件应该有一个PropertyPlaceholderConfigurer
:然后您可以在
beans.xml
中的其他位置引用这些属性:查看文章关于这一点,请查看 http://almaer.com /blog/spring-propertyplaceholderconfigurer-a-nice-clean-way-to-share
Your
beans.xml
file should have aPropertyPlaceholderConfigurer
:And then you can reference the properties as such elsewhere in the
beans.xml
:For an article about this, check out http://almaer.com/blog/spring-propertyplaceholderconfigurer-a-nice-clean-way-to-share
例如通过 PropertiesFactoryBean。使用 PropertyPlaceholderConfigurer< /a> 通过属性在上下文中配置 bean。
您将在其他答案中找到 PropertyPlaceholderConfigurer 示例。这是一个 PropertiesFactoryBean 示例:
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 :
有一个叫做 PropertyPlaceholderConfigurer 的东西,您可以使用它向您的 bean 注入属性文件中的值,如下所示:
There's this thing called a PropertyPlaceholderConfigurer, you can use it to inject your beans with values from a properties file, like this:
我们使用这个:
它允许在那里定义的属性用作配置文件中的引用
有关更多信息,请参阅:
http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/beans/factory/config/ PropertyPlaceholderConfigurer.html
We use this :
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
如果您想将该对象作为
java.util.Properties
的实例引用,您应该执行以下操作:这允许您将 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:This allows you to reference the Spring bean
properties
as an instance ofjava.util.Properties
. You can even have it merge together multiple properties files by adding more values tolocation
. 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.