且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以使用CDI注入EJB实现而不是其接口?

更新时间:2022-12-04 11:33:35

是的,你可以,但是因为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
}



参见: