使用guice动态绑定实例 [英] Dynamically bind instances using guice

查看:137
本文介绍了使用guice动态绑定实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序(独立的Apache骆驼)中,我必须绑定多个bean(pojos实例)。
因为这些pojos不能直接使用(在Java中),而是必须通过URL中的绑定引用来使用,所以我想注册枚举中的所有可用bean。然后将这些bean这样绑定:

In my application (stand alone apache camel) i have to bind several beans (instances of pojos). Because those pojos could not be used directly (in java) but have to be used via bound references in urls i want to "register" all available beans in an enum. The beans are then bound like this:

public class BeanRegistry extends JndiRegistry {
    public BeanRegistry() {
        for (Beans bean : Beans.values()) {
            try {
                this.bind(bean.name(), bean.clazz().newInstance());
            } catch (InstantiationException | IllegalAccessException e) {
                throw new IllegalStateException("Problem on instantiating bean " + bean.name() + " with type "
                                                + bean.clazz().getName() + ", cause Exception: ", e);
            }
        }

    }


    public static enum Beans {
        sorter(SortingStrategy.class),
        policy(PolicyForStartAndStopRoutes.class),
        doneFilter(ExcludeDoneFilesFilter.class);

        private final Class<?> clazz;

        Beans(Class<?> clazz) {
            this.clazz = clazz;
        }

        public Class<?> clazz() {
            return clazz;
        }
    }
}

使用此命令不会出现拼写错误只要使用enum的名称引用bean,就会发生这种情况。
我的问题是 bean.clazz()。newInstance()。有没有一种使用guice来提供实例的方法?有了guice,我可以将实例绑定到任意构造函数或实现。

With this no spelling mistakes could happen as long as you use enum's name to reference a bean. My problem is bean.clazz().newInstance(). Is there a way to use guice to "provide" the instances? With guice i could bind the instances to arbitrary constructors or "implementations".

推荐答案

我找到了使用 MapBinder 。它包装在单独的依赖项中:

I found a solution using MapBinder. It is packed in a separate dependency:

<dependency>
    <groupId>com.google.inject.extensions</groupId>
    <artifactId>guice-multibindings</artifactId>
    <version>3.0</version>
</dependency>

这是我的新代码(显示了我的guice模块),它与我的其他问题

And here is my new code (it shows my guice module) it is related to my other question:

@Override
protected final void configure() {
    ....
    // bind beans to instances
    MapBinder<String, Object> boundBeans = MapBinder.newMapBinder(binder(), String.class, Object.class);
    for (Beans bean : Beans.values()) {
        boundBeans.addBinding(bean.name()).to(bean.clazz());
    }
}

/**
 *
 * @param boundBeans all beans bound via a {@link MapBinder}
 * @return a jndi registry with all beans (from {@link Beans}) bound 
 */
@Provides
@Inject
final Registry getRegistry(final Map<String, Object> boundBeans) {
    JndiRegistry registry = new JndiRegistry();
    for (Entry<String, Object> boundBean : boundBeans.entrySet()) {
        registry.bind(boundBean.getKey(), boundBean.getValue());
    }
    return registry;
}

这篇关于使用guice动态绑定实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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