这是我的类(class):
public class Foo {
private static Properties conf;
}
如果我手动加载conf
,我可以这样做(为了清楚起见,省略了错误处理代码):
public class Foo {
private static Properties conf;
static {
InputStream is =... // Get the properties file as Stream from classpath
conf = new Properties();
conf.load(is);
// ...
}
}
现在,我想删除所有这些垃圾,如下所示:
public class Foo {
@javax.annotation.Resource(name="my-file-on-classpath.properties")
private static Properties conf;
}
我知道我可以使用 Spring 或类似的东西来做到这一点。
但是,是否可以仅使用 JDK 来完成此任务?
最佳答案
使用 CDI 需要 CDI 容器。这不包含在 JavaSE 中。例如,您必须使用额外的一个,例如 Spring。它在 JavaEE 6 中定义。
关于java - 如何仅使用 JDK 注入(inject)属性文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20053023/