Reputation: 88757
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:
EjbA
": this is not possible, since there are other EJBs which are neededEjbA
from the lib": that would require rather large changes in our libraries and the applications that use themEjbA
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 changesEjbA
": 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:
EjbA
so that the only implementation of IEjb
would be EjbB
and dependency injection would be fineEjbB
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).
Upvotes: 0
Views: 545
Reputation: 534
A littlebit late, but remember: EJB supports transactions, where as CDI-Beans not. But you could wrap the injection of an EJB in a corresponding CDI-bean, that proxies all methods of the EJB. To get enother EJB you'll write a @Specializes-CDI for it. Example (just to show, what I mean).
The Baseimplementation looks like this:
@Stateless // or whatever
class BaseEjb {
public void doSomeMagic() {...}
}
@Named
class BaseCdi {
@EJB
private BaseEjb ejb;
public void doSomeMagic() { ejb.doSomeMagic(); }
}
The customisation works like this:
@Stateless // or whatever
class SpecializedEjb extends BaseEjb {...}
@Specializes
class SpecializedCdi extends BaseCdi {
@EJB
private SpecializedEjb ejb;
public void doSomeMagic() { ejb.doSomeMagic(); }
}
Upvotes: 1
Reputation: 38152
A couple of ideas:
Upvotes: 1