我对 Jersey (2.18) 应用程序有以下实现:
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) {
}
}
}
并在点击注入存储库的服务时获得以下异常
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
如果我注释掉bindFactory
并使用注释掉的绑定,这一切都有效。我在工厂实施方面遗漏了什么吗?该异常似乎甚至在RepositoryFactory
构造函数被命中之前就发生了。我需要工厂,因为我要对OracleRepository
实例进行一些其他初始化。