Guice, Injecting TypeLiteral<T> when using @AssistedInject

杀马特。学长 韩版系。学妹 提交于 2019-12-24 02:17:05

问题


This works:

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();
    }
}

Guice automatically injects the TypeLiteral representing String when I do:

@Inject SomeGenericType<String> foo;

But when trying the same thing with 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) {
        ....

My Module looking like this:

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

I get an exception while installing the module:

TypeLiteral<T> cannot be used as a Key, it is not fully specified. 

It's certainly the TypeLiteral that I'm trying to Inject that is the problem, as the generic factory does work fine when I remove it.

So, I'll probably just roll my own factory for the moment, but I'm curious to whether this should work? Is it a matter of using the FactoryModuleBuilder slightly differently?


回答1:


How are you accessing the instance of FooFactory? I built the variation on your code below and it worked for me:

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>>() {} ) );
    }
}

Outputs:

class java.lang.String
myname

Note I used a regular @Inject annotation rather than @AssistedInject which I think is for multiple constructors in a factory. This also works if you inject the instance directly:

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" );
    }
}

Outputs:

class java.lang.String
test


来源:https://stackoverflow.com/questions/10219227/guice-injecting-typeliteralt-when-using-assistedinject

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!