如何使用 CDI 从 .properties 文件中 @Inject 值

How to @Inject values from a .properties file with CDI

如何从属性文件中注入一些值? 我有一个包含一些键和值的 .properties 文件,我想注入这些以在某些 类 上使用,例如:

@Inject(file = "/WEB-INF/abc.properties",key ="path")
private String path;

因此,当我使用路径时,它具有属性文件中的值,而不是彻底读取属性并获取值的整个过程。

虽然很快就有关于 integrating configuration in CDI, the current status is that there won't be a standard Java configuration JSR 的积极讨论。

同时,配置集成由第三方如DeltaSpike, see http://deltaspike.apache.org/documentation/configuration.html提供,因此可以编写:

@Inject
@ConfigProperty(name = "endpoint.poll.interval")
private Integer pollInterval;

这依赖于服务加载器机制来扩展配置源。

还有Apache Tamaya project plans to have CDI integration