Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In my JavaEE project I have an interface like this:

@Local
public interface DataAccess { ... }

And two stateless beans implementing it:

@Stateless
public class DataAccess_Online implements DataAccess { ... }

@Stateless
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:

@EJB
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
add comment

2 Answers

up vote 2 down vote accepted

You can use:

@Stateless(name="online")
public class DataAccess_Online implements DataAccess { ... }

@EJB(beanName="online")
DataAccess da;
share|improve this answer
    
Thanks, that is even simpler :) –  Puckl Feb 24 '13 at 16:45
add comment

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
add comment

Your Answer

 
discard

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.