代码优先:
@Slf4j
public class HelperModule extends AbstractModule
{
@Override
protected void configure()
{
}
@Provides
@Named("plugin")
@Singleton
public Reflections getReflections(@Nullable @Named("plugin-scope") String pluginScope)
{
if (pluginScope == null)
{
pluginScope = "me.wener";
}
log.info("Create plugin scanner for '{}'", pluginScope);
return new Reflections(new ConfigurationBuilder()
.setUrls(ClasspathHelper.forPackage(pluginScope))
.addScanners(
new SubTypesScanner(),
new TypeAnnotationsScanner())
// .filterInputsBy(new FilterBuilder().include(".*\\.java").exclude(".*"))
);
}
}
我期望的是pluginScope
可以为空且可选,我按照Use @Nullable指令操作,但这不起作用,我仍然有
No implementation for java.lang.String annotated with @com.google.inject.name.Named(value=plugin-scope) was bound
出了什么问题?
修改
顺便说一句,@Nullable
是javax.annotation.Nullable
答案 0 :(得分:3)
@Nullable
注释意味着您可以绑定到返回null
的提供者,而不是绑定可能不存在。你的@Nullable注释是允许以下工作的:
bind(String.class).annotatedWith(Names.named("plugin-scope"))
.toProvider(Providers.<String>of(null));
根据internal Guice error message,您明确必须绑定到null
的提供者,而不是toInstance(null)
。