Search code examples
ejb-3.0java-ee-6jboss7.xcdi

JEE6 : Alternative EJB declaration


I'm using JBoss 7.1.1 with CDI.

I've got a Stateless bean named ServiceAccount in JNDI. This is the real service implementation. I've got another Statelss bean named ServiceAccountMock which is a Mock service. The both herited from the same interface and are packaged in a service.ear.

What I want to do is to declare the mock service as alternative in bean.xml, redeploy my services ear, and then all the client see the mock version (without changing anything on client side).

When I deploy my service.ear, JBoss says :

java.lang.IllegalArgumentException: JBAS011046: A component named 'ServiceAccount' is already defined in this module

This is true, both services are declared the same way (@Stateless(name="ServiceAccount")).

If I change the name of the mock version, I have to change on client side which EJB is used (and I don't want to do that).

Does anyone know how to do that ?


Solution

  • I don't think you will be able to deploy 2 beans with the same name in the same application.

    If the clients of the bean are only local, you should use CDI type injection selection.

    • Remove the name of the beans or put different name if you realy need a name (The mock will have a different name that the real implementation).
    • Keep the @Alternative annotation on the mock.
    • At the injection point, use the interface as the type of variable (and probably using the @Inject annotation instead of the @EJB one may help).

    The EJB specification and CDI aren't yet completly aligned. EJB has some element like the name that need to be unique over the application and is not taken into account in the CDI alternative functionaltity.

    So I don't think you will be able to mix EJB name injection selection and CDI alternative injection selection.