Java EE 7 CDI手动实例化 [英] Java EE 7 CDI Manual Instantiation
本文介绍了Java EE 7 CDI手动实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有@Inject注释字段的类。我使用反射实例化类,但我希望CDI注入实例以执行类实例字段。是否有这样做它的方法吗?
I have a Class with fields annotated with @Inject. I instantiate the Class using reflection, but I want the CDI to inject instances to do the Class instance fields. Is there a way of doing it?
Object myInstanceWithDependecies = Class.forName("com.package.MyClass").newInstance();
CDI.injectAll(myInstanceWithDependecies);//This is what i want
是否有人知道如何做到这一点吗?如果有办法不使用反射扫描每个字段,我将不胜感激。
Does someone know how to do this? I would appreciate if there was a way of doing it without scanning each field using reflection.
提前致谢。
推荐答案
这可以完成工作
SomeBean bean = CDI .current()。select(SomeBean.class).get();
SomeBean bean = CDI.current().select(SomeBean.class).get();
这篇关于Java EE 7 CDI手动实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文