简体   繁体   中英

Guice - Binding annotation configuration error

I have a CustomInterface with two implementation classes ie InterfaceImplA and InterfaceImplB. I am just trying to inject these two in their respective fields in the manager class methods where implA should have InterfaceImplA injected and implB should have InterfaceImplB injected.

I have a manager class defined as

    @RequiredArgsConstructor(onConstructor = @__(@Inject))
    public class CustomManager {
     
        private @NonNull
        @interfaceAnnotationA
        final CustomInterface implA;
    
        private @NonNull
        @interfaceAnnotationB
        final CustomInterface implB;
    }

where CustomInterface is an interface containing two methods. I have binding annotations defined seperately for both Impl classes as follows

    @BindingAnnotation
    @Target({ FIELD, PARAMETER, METHOD })
    @Retention(RUNTIME)
    public @interface interfaceAnnotationA {}
    
    @BindingAnnotation
    @Target({ FIELD, PARAMETER, METHOD })
    @Retention(RUNTIME)
    public @interface interfaceAnnotationB {}

And finally my module class binds these classes as follows

    public class CustomModule extends AbstractModule {
    @Override
    protected void configure() {
         bind(CustomInterface.class).
            annotatedWith(interfaceAnnotationA.class).to(InterfaceImplA.class);
         bind(CustomInterface.class).
            annotatedWith(interfaceAnnotationB.class).to(InterfaceImplB.class);
        }
    }

However at runtime im getting the error as follows

com.google.inject.ConfigurationException: Guice configuration errors:

1) No implementation for com.custom.CustomInterface was bound.
  while locating com.custom.CustomInterface
    for parameter 2 at com.custom.manager.CustomManager.<init>(CustomManager.java:111)
  while locating com.custom.manager.CustomManager
    for parameter 0 at com.custom.Activity.<init>(Activity.java:44)
  while locating com.custom.Activity


1) No implementation for com.custom.CustomInterface was bound.
  while locating com.custom.CustomInterface
    for parameter 3 at com.custom.manager.CustomManager.<init>(CustomManager.java:111)
  while locating com.custom.manager.CustomManager
    for parameter 0 at com.custom.Activity.<init>(Activity.java:44)
  while locating com.custom.Activity
2 errors

I am pretty sure i am missing something in the binding code above. ANy help will be appreciated.

You are binding the types , but no implementation.

You can additionally provide the implementation either via a @Provides method:

@Provides interfaceImplA provideInterffaceImplA() { ... }

Or via an explicit binding inside the module:

bind(interfaceImplA.class).toInstance(...);
// Or
bind(interfaceImplA.class).toProvider(...);
// Etc.

I found the problem. Seems like lombok

@RequiredArgsConstructor(onConstructor = @__(@Inject))

on the Manager class is removing the annotations @interfaceAnnotationA & @interfaceAnnotationB from the generated code.

I had to remove @RequiredArgsConstructor and write the constructor manually as follows

    @Inject
    public CustomManager(@interfaceAnnotationA  CustomInterface implA,
                         @interfaceAnnotationB  CustomInterface implB) {
        this.implA = implA;
        this.implB = implB;
    }

I wonder if there is a way to make guice binding annotations work with lombok @RequiredArgsConstructor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM