是否可以使用CDI注入EJB实现而不是其接口? [英] Is it possible to inject EJB implementation and not its interface using CDI?
问题描述
我的配置是:Wildfly 8.2.0,Weld
My configuration is: Wildfly 8.2.0, Weld
是否可以在bean中注入而不是在CDI的接口中注入?
Is it possible to inject in bean and not in its interface in CDI ?
@Stateless
class Bean implements IBean {
...
}
interface IBean {
...
}
@SessionScoped
class Scoped {
@Inject
Bean bean; //Fail
@Inject
IBean iBean; //OK
}
编辑:
我在上一个问题中的更多信息:
无状态EJB实现接口注入失败
More Info in my previous question: Stateless EJB implements interface injection failed
推荐答案
是的,你可以,但是因为EJB只注入业务视图您正在公开的业务视图是 @Local
视图,这是您实现接口时的默认视图( IBean
在您的情况下是一个本地业务接口)。因此,如果您想要自己注入bean,则需要告诉容器您正在使用无界面视图。
Yes you can, but as EJB inject the business view the only business view you are exposing is the @Local
view which is the default when you implement an interface (IBean
in your case is a local business interface). So, if you want to inject the bean itself, you need to tell the container that you are using the no-interface view.
在您的示例中,如果您仍然想要实现你的接口并注入 Bean
你应该使用 @LocalBean
注释,这意味着bean暴露了一个无接口查看:
In your example, if you still want to implement your interface and inject Bean
you should use the @LocalBean
annotation which means that the bean exposes a no-interface view:
@Stateless
@LocalBean // <-- no-interface view
class Bean implements IBean {
...
}
interface IBean {
....
}
@SessionScoped
class Scoped {
@Inject
Bean bean; //Should be OK
}
或者,如果你不想实施任何接口,然后bean默认定义一个No-Interface视图:
Or, If you don't want to implement any interface, then the bean defines by default a No-Interface view:
@Stateless
class Bean {
...
}
@SessionScoped
class Scoped {
@Inject
Bean bean; //OK
}
参见:
- 什么是EJB中的本地/远程和无界面视图?
- 定义EJB 3.1视图(本地,远程,无接口)
- EJB 3.1 @LocalBean vs no annotation
- What is local/remote and no-interface view in EJB?
- Defining EJB 3.1 Views (Local, Remote, No-Interface)
- EJB 3.1 @LocalBean vs no annotation
See also:
这篇关于是否可以使用CDI注入EJB实现而不是其接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!