是否可以使用CDI注入EJB实现而不是其接口? [英] Is it possible to inject EJB implementation and not its interface using CDI?

查看:97
本文介绍了是否可以使用CDI注入EJB实现而不是其接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的配置是: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
}



参见:



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆