【发布时间】: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。