Guice,在使用@AssistedInject时注入TypeLiteral <T>

aid*_*nok 2 guice guice-3

这有效:

public static class SomeGenericType<T> {
    private TypeLiteral<T> type; 

    @Inject
    public SomeGenericType(TypeLiteral<T> type) {
        this.type = type; 
    }

    public Class<? super T> getType() {
        return type.getRawType();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我这样做时,Guice自动注入表示字符串的TypeLiteral:

@Inject SomeGenericType<String> foo;
Run Code Online (Sandbox Code Playgroud)

但是在使用Assisted Inject尝试相同的操作时:

public static interface FooFactory<T> {
    Foo<T> create(String name);
}

public static class Foo<T> {

    @AssistedInject
    public Foo(TypeLiteral<T> type, @Assisted String name) {
        ....
Run Code Online (Sandbox Code Playgroud)

我的模块如下所示:

public static class TestMod extends AbstractModule {
    @Override
    protected void configure() {
        install(new FactoryModuleBuilder().build(new TypeLiteral<FooFactory<String>>(){}));
    }   
}
Run Code Online (Sandbox Code Playgroud)

安装模块时出现异常:

TypeLiteral<T> cannot be used as a Key, it is not fully specified. 
Run Code Online (Sandbox Code Playgroud)

我要注入的当然是TypeLiteral,这是问题所在,因为在删除通用工厂时,它的工作正常。

所以,我暂时可能只是自己开工厂,但是我很好奇这是否可行?使用FactoryModuleBuilder会有一点不同吗?

Rya*_*son 5

您如何访问FooFactory实例?我在下面的代码上构建了变体,并且对我有用:

public class AnotherGuiceTest {
    public static void main( String[] args ) {
        Injector i = Guice.createInjector( new TestMod() );
        FooFactory<String> ff = i.getInstance( Key.get( new TypeLiteral<FooFactory<String>>() {} ) );
        ff.create( "myname" );
    }
}

interface FooFactory<T> {
    Foo<T> create( String name );
}

class Foo<T> {

    @Inject
    public Foo( TypeLiteral<T> type, @Assisted String name ) {
        System.out.println( type.getRawType() );
        System.out.println( name );
    }
}

class TestMod extends AbstractModule {
    @Override
    protected void configure() {
        install( new FactoryModuleBuilder().build( new TypeLiteral<FooFactory<String>>() {} ) );
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

class java.lang.String
myname
Run Code Online (Sandbox Code Playgroud)

请注意我用了一个普通@Inject的注释,而不是@AssistedInject,我认为是在工厂多个构造。如果您直接注入实例,这也可以:

public class AnotherGuiceTest {
    public static void main( String[] args ) {
        Injector i = Guice.createInjector( new TestMod() );
        AppClass ac = i.getInstance( AppClass.class );
    }
}

class AppClass {
    @Inject
    public AppClass( FooFactory<String> fooFactory ) {
        fooFactory.create( "test" );
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

class java.lang.String
test
Run Code Online (Sandbox Code Playgroud)