是否有一些简单的方法将从类路径加载文件的Properties类注入到EJB(3.1)中?
像这样的东西:
@Resource(name="filename.properties", loader=some.properties.loader)
private Properties someProperties;
Run Code Online (Sandbox Code Playgroud)
谢谢,
博佐
正如 bkail 所说,您可以通过以下方式实现这一目标。我不确定你的loader=some.properties.loader
真正意思是什么,所以跳过了做任何事情,但提供了选项,以防你想使用loader.getClass().getResourceAsStream ("filename.properties");
首先定义您的注射类型
@BindingType
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD,
ElementType.PARAMETER })
public @interface PropertiesResource {
@Nonbinding
public String name();
@Nonbinding
public String loader();
}
Run Code Online (Sandbox Code Playgroud)
然后为此创建一个生产者
public class PropertiesResourceLoader {
@Produces
@PropertiesResource(name = "", loader = "")
Properties loadProperties(InjectionPoint ip) {
System.out.println("-- called PropertiesResource loader");
PropertiesResource annotation = ip.getAnnotated().getAnnotation(
PropertiesResource.class);
String fileName = annotation.name();
String loader = annotation.loader();
Properties props = null;
// Load the properties from file
URL url = null;
url = Thread.currentThread().getContextClassLoader()
.getResource(fileName);
if (url != null) {
props = new Properties();
try {
props.load(url.openStream());
} catch (IOException e) {
e.printStackTrace();
}
}
return props;
}
}
Run Code Online (Sandbox Code Playgroud)
然后将其注入您的命名组件中。
@Inject
@PropertiesResource(name = "filename.properties", loader = "")
private Properties props;
Run Code Online (Sandbox Code Playgroud)
我这样做是为了查看焊接文档,其中以 @HttpParam 为例。这是根据焊接1.1.0,在焊接1.0.0中,获取注释可以这样完成
PropertiesResource annotation = ip.getAnnotation(PropertiesResource.class);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4778 次 |
最近记录: |