java Spring 可以解析和注入属性文件吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/106402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:03:33  来源:igfitidea点击:

Can Spring Parse and Inject Properties Files?

javaspringconfiguration-files

提问by Garth Gilmour

I already know how to:

我已经知道如何:

Load properties files into my Spring configuration using:

使用以下命令将属性文件加载到我的 Spring 配置中:

<context:property-placeholder location="aaa/bbb/ccc/stuff.properties"/> 

Build properties objects on the fly using:

使用以下方法即时构建属性对象:

<props><prop key="abc">some value</prop></props>

But what I cant do, and would be really useful, is to have Spring load a properties file and then build the matching properties object. I could then inject this into a bean in the normal way.

但是我不能做的并且非常有用的是让 Spring 加载一个属性文件,然后构建匹配的属性对象。然后我可以以正常方式将其注入到 bean 中。

I've searched for this elsewhere without success. Any ideas?

我在别处搜索过这个,但没有成功。有任何想法吗?

回答by toolkit

Take a look at util:properties

看看util:properties

<util:properties id="myProperties" location="classpath:com/foo/my.properties"/>

Then, to inject the Properties into your Spring-managed Bean, it's as simple as this:

然后,要将属性注入到 Spring 管理的 Bean 中,就像这样简单:

@Resource(name = "myProperties")
private Properties myProperties;