gpt4 book ai didi

java - 在 Guice 中使用通配符绑定(bind)泛型

转载 作者:行者123 更新时间:2023-11-30 11:42:21 25 4
gpt4 key购买 nike

我正在尝试绑定(bind) Class<? extends SuperClass>SubClass.class , 但我找不到合适的东西。

实际上我的绑定(bind)需要在 Set<Class<? extends SuperClass>> 中,所以我测试了普通绑定(bind)和多重绑定(bind),但我无法让它们中的任何一个正常工作。我想我正确地遵循了这个问题:Using Guice, how can I inject a bounded-wildcard class?但我不太确定,因为我的测试没有通过。

import static java.util.Collections.*;
import static org.junit.Assert.*;
import java.util.Set;
import org.junit.Test;
import com.google.inject.*;
import com.google.inject.multibindings.Multibinder;

public class GuiceWildcardTest {

@Test public void binderShouldBindClassWithWildcard() {
Guice.createInjector(new AbstractModule() {
@Override protected void configure() {
bind(new TypeLiteral<Class<? extends SuperClass>>() {}).toInstance(SubClass.class);
ClassHolder holder = new ClassHolder();
requestInjection(holder);
assertEquals(SubClass.class, holder.type);
}
});
}

@Test public void multibinderShouldBindSetOfClassesWithWildcard() {
Guice.createInjector(new AbstractModule() {
@Override protected void configure() {
Multibinder.newSetBinder(binder(), new TypeLiteral<Class<? extends SuperClass>>() {}).addBinding().toInstance(SubClass.class);
SetHolder holder = new SetHolder();
requestInjection(holder);
assertEquals(singleton(SubClass.class), holder.types);
}
});
}

static class SuperClass {}

static class SubClass extends SuperClass {}

static class ClassHolder {
@Inject Class<? extends SuperClass> type;
}

static class SetHolder {
@Inject Set<Class<? extends SuperClass>> types;
}
}

最佳答案

我的问题是我没有等到注入(inject)器完成。 requestInjection()可以解决问题,但是 Injector 之后已创建,因此不会在调用 requestInjection() 之后立即创建.

此代码有效:

import static java.util.Collections.*;
import static org.junit.Assert.*;

import java.util.Set;

import org.junit.Test;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.TypeLiteral;
import com.google.inject.multibindings.Multibinder;

public class GuiceWildcardTest {

@Test public void binderShouldBindClassWithWildcard() {
final ClassHolder holder = new ClassHolder();
Guice.createInjector(new AbstractModule() {
@Override protected void configure() {
bind(new TypeLiteral<Class<? extends SuperClass>>() {}).toInstance(SubClass.class);
requestInjection(holder);
}
});
assertEquals(SubClass.class, holder.type);
}

@Test public void multibinderShouldBindSetOfClassesWithWildcard() {
final SetHolder holder = new SetHolder();
Guice.createInjector(new AbstractModule() {
@Override protected void configure() {
Multibinder.newSetBinder(binder(), new TypeLiteral<Class<? extends SuperClass>>() {}).addBinding().toInstance(SubClass.class);
requestInjection(holder);
}
});
assertEquals(singleton(SubClass.class), holder.types);
}

static class SuperClass {}

static class SubClass extends SuperClass {}

static class ClassHolder {
@Inject Class<? extends SuperClass> type;
}

static class SetHolder {
@Inject Set<Class<? extends SuperClass>> types;
}
}

关于java - 在 Guice 中使用通配符绑定(bind)泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11748966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com