Guice,注入TypeLiteral T.使用@AssistedInject时 [英] Guice, Injecting TypeLiteral<T> when using @AssistedInject

查看:112
本文介绍了Guice,注入TypeLiteral T.使用@AssistedInject时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有效:

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会自动注入表示String的TypeLiteral:

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) {
        ....

我的模块如下所示:

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. 

肯定是我要注入的TypeLiteral,因为通用工厂在删除它时可以正常工作.

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.

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

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?

推荐答案

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

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

输出:

class java.lang.String
myname

注意,我使用常规的@Inject注释,而不是认为是工厂中多个构造函数的@AssistedInject.如果您直接注入实例,这也可以工作:

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

输出:

class java.lang.String
test

这篇关于Guice,注入TypeLiteral T.使用@AssistedInject时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆