Following the documentation of OptionalBinder
An API to bind optional values, optionally with a default value. OptionalBinder fulfills two roles:
- It allows a framework to define an injection point that may or may not be bound by users.
- It allows a framework to supply a default value that can be changed by users.
I am trying to follow up on the first point above for which I have the following setup:
interface Reporting<R> {} // service to be bind optionally
class InternalServiceImpl implements InternalService {
@Inject
Reporting reporting;
... // use this in a method
}
public class FrameworkModule extends AbstractModule {
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), Reporting.class);
}
}
in the user modules(class UserWorkingModule
) if I do not provide a binding such as
bind(new TypeLiteral<Reporting<ReportingEvent>>(){}).to(ReportingImpl.class).in(Singleton.class);
the application fails to start with the following logs:
1) No implementation for Reporting was bound. while locating Reporting for field at InternalServiceImpl.reporting(InternalServiceImpl.java:21) at FrameworkModule.configure(FrameworkModule.java:55) (via modules: UserWorkingModule -> FrameworkModule)
Is it still a must to provide a binding for Reporting
in the UserWorkingModule
?