Search code examples
javajakarta-eedependency-injectionjboss7.xcdi

Specialize EJB in JBoss


I asked this a while ago within a Jboss 4.2.3 (JavaEE 5) context and got the answer to try Spring for configuration, but I'd rather not add another framework when it isn't necessary.

Now with JavaEE 6 and 7 out I have the feeling that there's a simpler solution for my problem. However I wasn't lucky searching yet so if you can get me on the right track, I'd really appreciate your help.

Here's the problem:

Suppose we have an EJB interface IEjb and a library that provides an implentation EjbA of that interface. There are several other libraries which provide further EJBs and a couple of those contain @EJB IEjb ref, i.e. they need a reference to en EJB which exposes interface IEjb.

Now, there is the need to completely replace EjbA with an alternate implementation EjbB, i.e. EjbA should never be using in the entire application.

There are a few options that we can't use or which would require rather large changes:

  • "don't deploy the library containing EjbA": this is not possible, since there are other EJBs which are needed
  • "extract EjbA from the lib": that would require rather large changes in our libraries and the applications that use them
  • "implement EjbA as a non-EJB, subclass it when needed and make the subclass an EJB": the same reasons as above apply, this would require rather large changes
  • "delete EjbA": there are other applications which need to use it, it's currently just one application which should use a replacement instead.

So there are a few ways that I could think of:

  • tell the server not to deploy EjbA so that the only implementation of IEjb would be EjbB and dependency injection would be fine
  • tell dependency injection to always use EjbB when an instance of IEjb is required (something like CDI's @Specializes but for EJBs - possible in JavaEE 7?)

Any ideas on this? Is this possible (I have the feeling that yes) and if so, how?


A note on the environment: we're currently running on JBoss 7.2.0, but if this would only be possible using Wildfly, this might be an option (although we'd prefer not to be forced to update already).


Solution

  • A couple of ideas:

    • delete EjbA if it mustn't be used anyway
    • remove the EJB annotations
    • use @Inject to inject local EJBs (recommended anyway). I think CDI lets you specify a default implementation, AFAIK.