I am new to Guice. I using Guice in conjunction with AWS SWF. My current structure in as below:
MainClass:
class MainClass {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new ClientModule(param1, param2));
injector = injector.createChildInjector(injector.getInstance(TestModule.class));
}
}
TestModule:
class TestModule extends AbstractModule {
@override
protected void configure() {
// create SWF service object
// Register Workflow worker and Activity worker
TempWorkflowClientExternalFactory fac = TempClientExternalFactoryImpl(swf_service, domain);
bind(TempWorkflowClientExternalFactory.class).annotatedWith(Names.named("ABC")).toInstance(fac);
}
}
Definition to class where I want to use it:
class Test {
@Inject
@Named("ABC")
TempWorkflowClientExternalFactoryImpl fac;
public void method() {
TempWorkflowClientExternal temp = fac.getClient("123");
temp.callWorkflowMethod() // This method is defined in SWF Workflow Activity class
}
}
However on execution I get the below error:
No implementation for <packageName>.TempWorkflowClientExternalFactoryImpl annotated with @com.google.inject.name.Named(value=ABC) was bound
I need the same object to be returned wherever TempWorkflowClientExternalFactory is injected. I tried looking up at Guice wiki/FAQ. Unfortunately I think I overlooking something or have misunderstood some concept.
If instead of binding, I use the below code in configure method after creating the fac object, my application works as expected.
TempWorkflowClientExternal temp = fac.getClient("123");
temp.callWorkflowMethod()
However when I try to bind, it fails.
Update:
If I perform the binding without the annotatedWith, the program executes but I can observe that binding is not done correctly as the fac object in Test class gets instantiated with default constructor and not the object I want to associate with
Additional Detail: TempWorkflowClientExternalFactoryImpl, TempWorkflowClientExternalFactory, TempWorkflowClientExternal are also autogenerated classes by AWS-SWF. I cannot modify their constructors or add any annotations in those classes. Test.method() will be invoked by when certain business logic criteria is met.
Can someone please assist how to resolve the error?
Injector? Is yourTestModuleactually installed in the injector? It would help if you could provide a minimal reproducible example. - Daniel Pryden