【问题标题】:Google Guice - how to automatically add bindingGoogle Guice - 如何自动添加绑定
【发布时间】:2018-10-21 13:05:55
【问题描述】:

在我的项目中,我使用Google Guice 进行依赖注入。在扩展 Google Guice 的 AbstactModule 的 Module 类中,我有一个 MapBinder,其中键是项目的名称,值是我的 Item 接口的实现(例如 FirstItem)。

MapBinder<String, Item> itemMapBinder = MapBinder.newMapBinder(binder(), String.class, Item.class);
itemMapBinder.addBinding("FirstItem").to(FirstItem.class);

目前,每次我添加新的 Item 实现(例如 SecondItem)时,我还必须在 Module 中添加一个新行,例如

itemMapBinder.addBinding("SecondItem").to(SecondItem.class);

我在我的 ItemFactory 中使用这个项目地图。

public class ItemFactoryImpl implements ItemFactory {

    private final Map<String, Item> items;

    @Inject
    public ItemFactoryImpl(Map<String, Item> items) {
        this.items = items;
    }

    @Override
    public Item getItem(String name) {
        if (name == null) throw new IllegalArgumentException("Name cannot be null");
        return items.get(name);
    }
}

我试图找到一种方法来为 Item 接口的新实现自动添加绑定。我的想法是使用自定义注释,它将添加到 Item 接口的新实现中,并以某种方式修改 Module#configure 以使用此自定义注释为所有类添加绑定。我完成了这样的事情:

// cz.milanhlinak.guiceautobinding.item.AutoBindableItem.java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoBindableItem {
}

// cz.milanhlinak.guiceautobinding.item.Item.java
public interface Item {
}

// cz.milanhlinak.guiceautobinding.item.SecondItem.java
@AutoBindableItem
public class SecondItem implements Item {
}

// cz.milanhlinak.guiceautobinding.Module.java
public class Module extends AbstractModule {

    @Override
    protected void configure() {

        MapBinder<String, Item> itemMapBinder = MapBinder.newMapBinder(binder(), String.class, Item.class);
        new Reflections("cz.milanhlinak.guiceautobinding.item")
            .getTypesAnnotatedWith(AutoBindableItem.class)
            .stream()
            .filter(Item.class::isAssignableFrom)
            .forEach(typeAnnotatedWith -> itemMapBinder
                    .addBinding(typeAnnotatedWith.getSimpleName())
                    .to((Class<? extends Item>) typeAnnotatedWith)
            );

        bind(ItemFactory.class).to(ItemFactoryImpl.class).in(Singleton.class);
    }
}

完整代码可在我的GitHub repository 中找到。

我想知道是否有更好的方法来实现与 Google Guice 的自动绑定,因为如您所见,我目前正在使用一个额外的库 - Reflections

更新

另一个选项可能是使用Google Guava 而不是Reflections

public class Module extends AbstractModule {

    @Override
    protected void configure() {

        MapBinder<String, Item> itemMapBinder = MapBinder.newMapBinder(binder(), String.class, Item.class);

        ClassPath classPath;
        try {
            classPath = ClassPath.from(Thread.currentThread().getContextClassLoader());
        } catch (IOException e) {
            throw new RuntimeException("Unable to read class path resources", e);
        }

        ImmutableSet<ClassPath.ClassInfo> topLevelClasses = classPath.getTopLevelClassesRecursive("cz.milanhlinak.guiceautobinding.item");
        topLevelClasses.stream()
                .map(ClassPath.ClassInfo::load)
                .filter(clazz -> clazz.isAnnotationPresent(AutoBindableItem.class) && Item.class.isAssignableFrom(clazz))
                .forEach(clazz -> itemMapBinder
                        .addBinding(clazz.getSimpleName())
                        .to((Class<? extends Item>) clazz));

        bind(ItemFactory.class).to(ItemFactoryImpl.class).in(Singleton.class);
    }
}

【问题讨论】:

  • 这不是一个糟糕的解决方案 imo。

标签: java guice


【解决方案1】:

你没有做错什么。

使用反射没有错。该工具非常出色,可以完成您想要的工作。

Guice 没有发现工具,因此只有在 Guice 中没有“默认”方式来做你想做的事情。

如果你想使用 Reflections 以外的东西,你可以使用 java.util.ServiceLoader,但 ServiceLoader 对 Guice 并不友好,因为它会自动创建实例,但你通常会使用 Guice,因为你希望它创建实例为你而不是让另一个框架为你做这件事。

使用 Guava 的 Classpath 的替代方案也很有意义,但反射功能更强大,因为您还可以加载不在类路径中的类。

归根结底,您已经做出了更好的选择。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    相关资源
    最近更新 更多