0

I had this class as follows which works fine

@Singleton
public class EmpResource {

private EmpService empService;

@Inject
    public EmpResource(EmpService empService) {
        this.empService=empService;
    }

}

public class EmpService {

public void getName(){..}

}

Now instead of using EmpService directly, I had to create an interface and EmpService implement that interface as follows.

public interface IEmpService{
 void getName();
}

public class EmpServiceImpl implements IEmpService {
public void getName(){...}
}

So now my resource class has to use the interface but I am not sure how to reference the implementation it has to use.

@Singleton
public class EmpResource {

private IEmpService empService;

@Inject
    public EmpResource(IEmpService empService) {
        this.empService=empService;
    }

}

I've seen this and I wasn't sure where my binding should go. (This is my first project related to Guice so I am a total newbie).

This is the error that came "No implementation for com.api.EmpService was bound." which is totally understandable but not sure how to fix it. I appericiate your help.

FYI: I am using Dropwizard application.

Community
  • 1
  • 1
WowBow
  • 7,137
  • 17
  • 65
  • 103
  • 3
    Do you have an implementation of interface `com.google.inject.Module` somewhere in your project (may be a subclass of `com.google.inject.AbstractModule`)? That's the place where to put your bindings. – Thomas Fritsch Jan 27 '17 at 16:51
  • Yes. There is one that extends Module. Actually I was looking at this example http://www.journaldev.com/2403/google-guice-dependency-injection-example-tutorial. So in this case if I put bind(MessageService.class).to(MockMessageService.class); in the configure method, will the implementation be available ? (assuming that dropwizard or guice keeps the object in the application) – WowBow Jan 27 '17 at 17:11
  • Yes, that should work. – Thomas Fritsch Jan 27 '17 at 17:37
  • 1
    May you please show how do you use the injector? Have a look at the example in SO documentation, maybe this will help too -> http://stackoverflow.com/documentation/guice/3449/getting-started-with-guice#t=201701280708570411054 – Lachezar Balev Jan 28 '17 at 07:09

2 Answers2

2

You would configure your module similar to this:

public class YourModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(EmpService.class).to(EmpServiceImpl.class);
        // ....
    }
}
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
  • Thomas I did that and still facing same error. I heard from a colleague that I need to use @Provides annotation on another method in the module. – WowBow Jan 27 '17 at 17:57
0

you also have to add a Provide Methode for your EmpServiceImpl class

public class MyModule  extends AbstractModule {

    @Override
    protected void configure() {
             bind(IEmpService.class).to(EmpServiceImpl.class);
    }

    @Provides
    EmpServiceImpl provideEmpServiceImpl()  {
        // create your Implementation here ... eg.
        return new EmpServiceImpl();
    } 

}
Michael Meyer
  • 2,179
  • 3
  • 24
  • 33