I have the following implementation for a Jersey (2.18) application:
public class RootApplication extends ResourceConfig { public RootApplication() { packages("com.foo.bar"); register(new AbstractBinder() { @Override protected void configure() { bindFactory(RepositoryFactory.class).to(Repository.class); // if I use following line instead of bindFactory it works // bind(OracleRepository.class).to(Repository.class); } }); } public class RepositoryFactory implements Factory<Repository> { private final Repository repo; public RepositoryFactory() { this.repo = new OracleRepository(); } @Override public Repository provide() { return repo; } @Override public void dispose(Repository repo) { } } } and get the exception below when hitting a service that injects Repository
javax.servlet.ServletException: A MultiException has 3 exceptions. They are: 1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=Repository,parent=MeasureService,qualifiers={},position=-1,optional=false,self=false,unqualified=null,56464420) 2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.fidelity.pi.dashboard.rest.MeasureService errors were found 3. java.lang.IllegalStateException: Unable to perform operation: resolve on com.fidelity.pi.dashboard.rest.MeasureService It all works if I comment out the bindFactory and use the commented-out bind. Am I missing something in terms of the Factory implementation? The exception seems to happen even before the RepositoryFactory constructor is hit. I need the factory since I have some other initialization to do on the OracleRepository instance.