Java-在内部注入Bean方法 [英] Java - Inject Bean inside method
问题描述
我想在我的方法中注入bean。我有远程方法(直接Web远程处理),我需要在此方法中注入一些bean。我无法在字段声明部分中使用@Inject注释,因为它无法工作。甚至有可能吗?
I want to inject bean inside my method. I have remote method(Direct Web Remoting) and i need to inject some bean inside this method. I can't use @Inject annotation in field declaration section because it will not work. It is even possible ?
推荐答案
有很多方法可以执行这样的操作。您运行什么容器? DWR似乎还不支持CDI。
There are lots of ways to do something like this. What container do you run? DWR doesn't seem to support CDI yet.
Apache DeltaSpike提供了一种手动注入bean的方法。一种方法是进行如下调用:
Apache DeltaSpike has a way you can manually inject beans. One way is to do a call like this:
EchoService echoService = BeanProvider.getContextualReference(EchoService.class,false);
另一种方法是在运行时丰富实际的类。看看 BeanManager
中的getBeans。
Another is to enrich the actual class at runtime. Take a look at getBeans in BeanManager
.
这篇关于Java-在内部注入Bean方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!