是否有一种方法在Guice中实现对带有自定义注释的构造函数参数的注入?
我的问题和这个问题几乎完全一样:构造函数参数的Guice自定义注入
不过,那是五年前的事了。我想看看是否有什么变化。
我需要基于像name这样的限定符来解析参数,但是这些名称必须在运行时被解析(也就是说,我不能只在@Named注释中编译)。
--一个人为的例子:
public class MyService
{
private final DataStore store;
@Inject
public MyService(@DataStoreType("sqlite") final DataStore store)
{
this.store = store;
}
...
}某种解决问题的动态解析器是这样的:
public DataStore resolve(final DataStoreType annotation)
{
if ("sqlite".equals(annotation.value())
{
return sqliteStore;
}
else if ("postgresql".equals(annotation.value())
{
return pgStore;
}
...
}对于那些熟悉HK2/Jersey的人,我正在寻找类似org.glassfish.hk2.api.InjectionResolver in Guice的东西
发布于 2020-11-13 07:32:35
这个案子可以用工厂来解决。只需将逻辑从resolve()方法移到Factory.create(DataStoreType)即可。
https://stackoverflow.com/questions/64719373
复制相似问题