Spring JavaConfig for java.util.Properties字段 [英] Spring JavaConfig for java.util.Properties field

查看:122
本文介绍了Spring JavaConfig for java.util.Properties字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


您能告诉我如何使用Spring Javaconfig直接将属性文件加载/自动连接到java.util.Properties字段吗?


Can you please tell me how to use Spring Javaconfig to directly load/autowire a properties file to a java.util.Properties field?

谢谢!

以后编辑-仍在寻找答案: 通过Spring JavaConfig是否可以将属性文件直接加载到java.util.Properties字段中?

Later edit - still searching for the answer: Is it possible to load with Spring JavaConfig a properties file directly into a java.util.Properties field?

推荐答案

XML基本方式:

在春季配置中:

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

在您的课堂上:

@Autowired
@Qualifier("myProperties")
private Properties myProperties;

仅JavaConfig

似乎有一个注释:

@PropertySource("classpath:com/foo/my-production.properties")

对此类进行注释将把文件中的属性加载到环境中.然后,您必须将Environment自动连接到类中以获取属性.

Annotating a class with this will load the properties from the file in to the Environment. You then have to autowire the Environment into the class to get the properties.

@Configuration
@PropertySource("classpath:com/foo/my-production.properties")
public class AppConfig {

@Autowired
private Environment env;

public void someMethod() {
    String prop = env.getProperty("my.prop.name");
    ...
}

我看不到将它们直接注入Java.util.properties的方法.但是您可以创建一个使用此批注的类,该批注充当包装器,并以此方式构建属性.

I do not see a way to directly inject them into the Java.util.properties. But you could create a class that uses this annotation that acts as a wrapper, and builds the properties that way.

这篇关于Spring JavaConfig for java.util.Properties字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆