简体   繁体   English

Guice - 绑定注解配置错误

[英]Guice - Binding annotation configuration error

I have a CustomInterface with two implementation classes ie InterfaceImplA and InterfaceImplB.我有一个CustomInterface,它有两个实现类,即InterfaceImplA 和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.我只是想在管理器 class 方法中将这两个注入各自的字段,其中 implA 应该注入 InterfaceImplA 而 implB 应该注入 InterfaceImplB。

I have a manager class defined as我有一个经理 class 定义为

    @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.其中 CustomInterface 是一个包含两个方法的接口。 I have binding annotations defined seperately for both Impl classes as follows我为两个 Impl 类分别定义了绑定注释,如下所示

    @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最后我的模块 class 将这些类绑定如下

    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方法提供实现:

@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.在 Manager class 上,正在从生成的代码中删除注释 @interfaceAnnotationA 和 @interfaceAnnotationB。

I had to remove @RequiredArgsConstructor and write the constructor manually as follows我必须删除 @RequiredArgsConstructor 并手动编写构造函数,如下所示

    @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我想知道是否有办法让 guice 绑定注释与 lombok @RequiredArgsConstructor 一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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