1

Code first:

@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(".*"))
        );
    }
}

What I expected is pluginScope can be null and optional, I follow the instruction Use @Nullable, but this is not works, I still got

No implementation for java.lang.String annotated with @com.google.inject.name.Named(value=plugin-scope) was bound

What's wrong ?

EDIT BTW,The @Nullable is javax.annotation.Nullable

wener
  • 7,191
  • 6
  • 54
  • 78

1 Answers1

4

A @Nullable annotation means that you can bind to a Provider that returns null, not that the binding can be absent. Your @Nullable annotation is what would allow the following to work:

bind(String.class).annotatedWith(Names.named("plugin-scope"))
    .toProvider(Providers.<String>of(null));

You explicitly must bind to a Provider of null, rather than toInstance(null), according to an internal Guice error message.

Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251