我有2个接口和2个实现,如下所示。
KafkaConsumerExecutor
(Interface)DefaultExecutor
(实施1)ConsumerRecordsProcessor
(Interface)LastOnlyRecordsProcessor
(Impl of 3.)
,
public interface KafkaConsumerExecutor {
void start();
void stop();
}
public class DefaultExecutor<MESSAGE_TYPE> implements KafkaConsumerExecutor {
private final ConsumerRecordsProcessor<MESSAGE_TYPE> consumerRecordsProcessor;
@Override
public void start(){}
//processStuff
}
@Override
public void stop(){}
//kill
}
public interface ConsumerRecordsProcessor<MESSAGE_TYPE> {
void process(Iterable<ConsumerRecord<String, MESSAGE_TYPE>> consumerRecords);
}
,
public class LastOnlyRecordsProcessor<MESSAGE_TYPE extends Serializable> implements ConsumerRecordsProcessor<MESSAGE_TYPE> {
public void process(Iterable<ConsumerRecord<String, MESSAGE_TYPE>> consumerRecords){
// process stuff.
}
}
最后,我有了调用start执行器的类。
@Inject
public KafkaListenerManagerImpl(ConsumerExecutor consumerExecutor) {
this.consumerExecutor= consumerExecutor;
}
我的问题是,我不能绑定我的通用实现DefaultExecutor<String>
到ConsumerExecutor
我试过的方法是;
1。
bind(new TypeLiteral<DefaultExecutor<String>>() {}).to(new TypeLiteral<ConsumerExecutor>(){});
bind(LastOnlyRecordsProcessor.class).to(new TypeLiteral<ConsumerRecordsProcessor<Request>>() {});
Caused by: java.lang.IllegalArgumentException: argument type mismatch
2。
bind(DefaultExecutor.class).to(new TypeLiteral<ConsumerExecutor>() {});
bind(LastOnlyRecordsProcessor.class).to(new TypeLiteral<ConsumerRecordsProcessor<String>>() {});
这个看起来更好,但是它找不到透视图,但是我没有为Processor绑定。
UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=ConsumerRecordsProcessor<MESSAGE_TYPE>,parent=DefaultExecutor,qualifiers={},position=1,optional=false,self=false,unqualified=null,1237264838)
bind(DefaultExecutor.class).to(new TypeLiteral<ConsumerExecutor>() {}); bind(new TypeLiteral<LastOnlyRecordsProcessor<String>>() {}).to(new TypeLiteral<ConsumerRecordsProcessor<String>>() {});
还是一样;
Caused by: org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=ConsumerRecordsProcessor<MESSAGE_TYPE>,parent=DefaultExecutor,qualifiers={},position=1,optional=false,self=false,unqualified=null,202064342)
问题简单来说就是,
- 我想把DefaultExecutor绑定到ConsumerExecutor。
- LastRecordsOnlyProcessor到ConsumerRecordsProcessor
我不抱太大希望,但是你们看到我哪里做错了吗?
提前感谢。
这可能是问题所在。
public class Main {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new MyModule());
KafkaListenerManagerImpl kafkaListenerManager = injector.getInstance(KafkaListenerManagerImpl.class);
}
}
class MyModule extends AbstractModule {
@Override
protected void configure() {
bind(new TypeLiteral<ConsumerExecutor<String>>(){}).to(DefaultExecutor.class);
bind(new TypeLiteral<ConsumerRecordsProcessor<String>>(){}).to(LastOnlyRecordsProcessor.class);
}
}
在本例中,MyModule
类将DefaultExecutor<String>
实现绑定到ConsumerExecutor
接口,将LastOnlyRecordsProcessor
实现绑定到ConsumerRecordsProcessor
接口。
变化;
bind(new TypeLiteral<ConsumerExecutor<String>>(){}).to(DefaultExecutor.class);
;
bind(ConsumerExecutor.class).to(DefaultExecutor.class);
和
bind(new TypeLiteral<ConsumerRecordsProcessor<String>>(){}).to(LastOnlyRecordsProcessor.class);
;
bind(ConsumerRecordsProcessor.class).to(LastOnlyRecordsProcessor.class);
你必须确保你导入了正确的依赖项和类。
此外,在KafkaListenerManagerImpl
类中,您应该注入ConsumerExecutor<String>
而不是ConsumerExecutor
:
@Inject
public KafkaListenerManagerImpl(ConsumerExecutor<String> consumerExecutor) {
this.consumerExecutor= consumerExecutor;
}
希望有帮助。