Guice Dependency Injection between different projects

Initially I started working on a Play! Java project that has a Controller, Processor and DAO. I used dependency injection using Google Guice's @ImplementedBy for my Processor interface and my ProcessorImpl implemented it.

Right now, I have created another project which also requires the Processor. So I extracted out the interface to another separate project, say common, and the two projects use that common project as a referenced library.

The problem is, I won't be able to use @ImplementedBy anymore since that common project will not have the two projects' references. Since that is not possible, I am not able to go for dependency injection. Without giving @ImplementedBy, I am getting the following error:

play.api.UnexpectedException: Unexpected exception[ProvisionException: Unable to provision, see the following errors:

1) No implementation for com.processor.Processor was bound.

Is there a way to configure the dependencies in a config file? Or can the dependency be injected in the implemented classes?

Upvotes: 0

Views: 1355

Answers (1)

vsbehere
vsbehere

Reputation: 666

Create a guice module in project where your ProcessorImpl is located.

public class Module extends AbstractModule {
    protected void configure() {
         bind(Processor.class).to(ProcessorImpl.class);
    }
}

Inject Processor wherever you need.

If you call this module Module and place it in the root package, it will automatically be registered with Play.

Upvotes: 1

Related Questions