Can I CDI-1.0 or -2.0-inject multiple interfaces implemented by one Stateless EJB?

896 views Asked by At

Like in the following example:

public interface CommandLineDieselEngineExhaustManipulatorService{
    // command line services
}
public interface ClientDieselEngineExhaustManipulatorFacade{
    // ui-client services
}


@Stateless
public class DieselEngineExhaustManipulatorImpl  implements CommandLineDieselEngineExhaustManipulatorService, ClientDieselEngineExhausManipulatorFacade {
    // Implementation of both interfaces
}

@Stateless
public class MyCdiManagedClass{

    @Inject 
    private CommandLineDieselEngineExhaustManipulatorService cliService;

    @Inject
    private ClientDieselEngineExhausManipulatorFacade clientFacade;

    // Whatever
}

I am not interested your opinion in whether or not I should favour the injection of plain Stateless EJBs (without interface) over Stateless EJBs injectable by its implementing interface.

I tried with JEE7 (CDI1) without success. I got the impression that EJB+CDI does not support that.

I wonder if it possible with JEE8?

I could not find any part in the CDI 2.0 specification, which seems to give a hint in the direction. I would highly appreciate if someone could point to the right place.

This Question did not answer the injection part either, might be outdated and the interesting link is dead: Can an EJB bean implement multiple interfaces?

Any ideas?

1

There are 1 answers

3
Siliarus On BEST ANSWER

So, first of all, CDI is not the bad guy here - in fact, from CDI viewpoint, having a bean with two interfaces, you can inject two beans based on those interfaces without problems.

The real deal seems to be EJB. Now, I am not sure what application server are you running and what EJB version (you tagged question as ejb-3.0 but talk about Java EE 7 which would be EJB 3.2).

From EJB 3.1 specification, section 4.9.7 Session Bean’s Business Interface:

If the bean does not expose any other client views (Local, Remote, No-Interface, 2.x Remote Home, 2.x Local Home, Web Service) and the bean class implements a single interface, that interface is assumed to be the business interface of the bean. This business interface will be a local interface unless the interface is designated as a remote business interface by use of the Remote annotation on the bean class or interface or by means of the deployment descriptor.

Long story's short - Multiple interfaces are not supported in EJB 3.1. That being said, some servers might support it?

However, moving on to EJB 3.2, again section 4.9.7 Session Bean’s Business Interface:

If the bean class is annotated with the Local annotation, or if the bean class is annotated with neither the Local nor the Remote annotation, all implemented interfaces (excluding the nterf aces listed above) are assumed to be local business interfaces of the bean.

Therefore, in EJB 3.2, it is supported. Note that just below this text there is an example with exactly your case.

So I guess your problem boils down to:

  • What application server do you use?
  • What EJB version does the server have?