在Guice中使用通配符绑定泛型

时间:2012-07-31 21:02:59

标签: java guice

我正在尝试将Class<? extends SuperClass>绑定到SubClass.class,但我找不到合适的内容。

实际上我的绑定需要在Set<Class<? extends SuperClass>>中,所以我测试了正常绑定和多绑定,但我不能让它们中的任何一个正常工作。我想我正确地遵循了这个问题: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;
    }
}

1 个答案:

答案 0 :(得分:1)

我的问题是我没有等待注射器完成。 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;
    }
}