在运行时更改Guice绑定

问题描述 投票:9回答:1

我希望能够在运行时更改Guice注入,以支持基于用户输入的多次注入。这就是我想要实现的目标:

public interface IDao {
    public int someMethod();
}

public class DaoEarth implements IDao {
    @Override
    public int someMethod(){ ... }
}

public class DaoMars implements IDao {
    @Override
    public int someMethod(){ ... }
}

public class MyClass {
    @Inject
    private IDao myDao;

    public int myMethod(String domain) {
        //If Domain == Earth, myDao should be of the type DaoEarth

        //If Domain == DaoMars, myDao should be of the type DaoMars
    }
}

我正在考虑编写自己的Provider,但我不知道如何使用该提供程序在运行时更改我的绑定。任何输入都是欢迎和赞赏:)!

更新这是我目前提出的,它不像我想的那么漂亮,所以我仍然在寻找反馈

public class DomainProvider {
    @Inject @Earth
    private IDaoProvider earthDaoProvider;

    @Inject @Mars
    private IDaoProvider marsDaoProvider;

    public IDaoProvider get(Domain domain){
        switch (domain){
            case EARTH:
                return earthDaoProvider;
            case MARS:
                return marsDaoProvider;
        }
    }

    public IDaoProvider get(String domain){
        Domain parsedDomain = Domain.valueOf(domain.toUpperCase());
        return get(parsedDomain);
    }
}

//MarsDaoProvider would be equivalent
public class EarthDaoProvider implements IDaoProvider {
    @Inject @Earth
    private IDao earthDao;

    public IDao getDao() {
        return earthDao;
    }
}

// This means that in "MyClass", I can do:
public class MyClass {
    @Inject
    private DomainProvider domainProvider;

    public int myMethod(String domain) {
        IDaoProvider daoProvider = domainProvider.get(domain);
        IDao dao = daoProvider.getDao();

        //Now "dao" will be of the correct type based on the domain
    }
}

//Of course elsewhere I have the bindings set like
bind(IDao.class).annotatedWith(Earth.class).to(EarthDao.class);
java architecture dependency-injection playframework-2.0 guice
1个回答
13
投票

你的版本几乎是完美的:你需要注入一些基于你编写的代码返回一个或另一个的对象,并且不需要辅助注入或类似的东西。也就是说,你可以跳过一些样板:

public class DomainProvider {
    // Just inject Providers directly without binding them explicitly.
    @Inject @Earth Provider<IDao> earthDaoProvider;
    @Inject @Mars Provider<IDao> marsDaoProvider;

    public Provider<IDao> get(Domain domain){
        switch (domain){
            case EARTH:
                return earthDaoProvider;
            case MARS:
                return marsDaoProvider;
        }
    }

    public Provider<IDao> get(String domain){
        Domain parsedDomain = Domain.valueOf(domain.toUpperCase());
        return get(parsedDomain);
    }
}

在这种情况下,你的MyClass将完全相同。在这里,Provider要么是单方法通用接口com.google.inject.Provider,要么是它扩展的等效内置javax.inject.Provider。在the relevant Guice wiki topic上阅读更多关于Guice Providers的信息。

bind(IDao.class).annotatedWith(Earth.class).to(EarthDao.class);
// You can now inject "@Earth IDao" and also "@Earth Provider<IDao>".

基本上,如果你绑定一个密钥Foo(一个类,提供者,@Provides方法或实例),你automatically get to inject either a Foo or Provider<Foo>没有额外的工作。提供者也是确保每次调用get时获得新实例的好方法,如果这是您想要的;使用原始版本,对于您注入的任何给定DomainProvider,您将始终获得相同的EarthDao或MarsDao实例。 (如果你有像@Singleton这样的范围绑定,Guice也会尊重它;提供者只是让Guice参与进来,而不是重用一个普通的旧Java引用。)

这意味着您可以跳过自定义的EarthDaoProvider和MarsDaoProvider,除非您确实需要对它们执行任何外部初始化 - 此时您最好调用bind(EarthDao.class).toProvider(EarthDaoProvider.class),以便在直接注入EarthDao时也可以进行准备。您也可以让DomainProvider直接通过在相应的Provider上调用get来返回IDao实例,并确保它每次都是一个新实例。

© www.soinside.com 2019 - 2024. All rights reserved.