Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In my JavaEE project I have an interface like this:

public interface DataAccess { ... }

And two stateless beans implementing it:

public class DataAccess_Online implements DataAccess { ... }

public class DataAccess_Offline implements DataAccess { ... }

And I get the exception:

Cannot resolve reference Local ejb-ref.... because there are 2 ejbs in the application with interface ...DataAccess

The problem is clear, there may only be one class implementing DataAccess. But it would be convenient if I could use two. In the client I want to work on the Interface DataAccess only, so I don´t have to distinguish between online/offline in my code, they both offer the same methods.

In the client I have an dependency injection:

DataAccess da;

And I see how this is a problem with two implementations for DataAccess, but what should I change to make it work? The client always starts with the online version of DataAccess, but after a while he could request the offline version, so the offline version only needs the same interface, if I need a DataAccess injection it will always be the online version.

How can I use two implementations of an interface together with @EJB dependency injection?

share|improve this question
up vote 3 down vote accepted

You can use:

public class DataAccess_Online implements DataAccess { ... }

DataAccess da;
share|improve this answer
Thanks, that is even simpler :) – Puckl Feb 24 '13 at 16:45

Well the solution was simple, I just had to introduce a new interface:

public interface DataAccessOffline extends DataAccess

The error is gone and I can inject into DataAccess with @EJB, but I can still switch between the online and offline implementations.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.