Is it possible to implement an EJB implementation rather than an interface using CDI?

My configuration: Wildfly 8.2.0, Weld

Is it possible to enter a bean and not its interface in CDI?

@Stateless class Bean implements IBean { ... } interface IBean { ... } @SessionScoped class Scoped { @Inject Bean bean; //Fail @Inject IBean iBean; //OK } 

EDIT :

Additional information in my previous question: Invalid EJB does not allow interface injection

+8
java dependency-injection cdi weld ejb
source share
2 answers

Yes, you can, but as EJB introduces the business view, the only kind of business that you publish is the @Local , which is used by default when implementing the interface ( IBean in your case is the local business interface). So, if you want to enter the bean itself, you need to tell the container that you are using a view without an interface.

In your example, if you still want to implement your interface and insert a Bean , you should use the @LocalBean annotation, which means the bean provides a view without an interface:

 @Stateless @LocalBean // <-- no-interface view class Bean implements IBean { ... } interface IBean { .... } @SessionScoped class Scoped { @Inject Bean bean; //Should be OK } 

Or, if you do not want to implement any interface, then the bean by default defines a view without an interface:

 @Stateless class Bean { ... } @SessionScoped class Scoped { @Inject Bean bean; //OK } 

See also:

+14
source share

It looks like you have an obscure answer in your previous question, and in fact this whole question is a continuation of this.

In general, CDI allows you to enter both an interface and an impl for managed CDI beans. This does not apply to EJB. When an EJB implements an interface, it becomes its business interface. Only methods specified there are valid. Effectively, your Bean class only defines how these methods work, and does not actually exist as a bean in your runtime.

Therefore, no, when using EJB you cannot implement the implementation, but only the interface. If you really want to do this, I would move away from EJB.

+2
source share

All Articles