我正在尝试向我的Rest服务控制器注入依赖,后者使用Jersey实现,
这是我的控制器
@Path("service")
public class ServiceProviderController {
@Inject
private ServiceProviderService prov;
public ServiceProviderController(){}
@GET
@Produces(MediaType.APPLICATION_JSON)
Public ServiceProviders testService(@QueryParam("name") String name) {
prov.getName(name);
return name;
}
这是活页夹类
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import com.api.controller.ServiceProviderController;
import com.api.service.ServiceProviderService;
public class APIBinder extends AbstractBinder {
@Override
protected void configure() {
bindAsContract(ServiceProviderService.class).to(ServiceProviderController.class);
} }
这是应用程序类
import org.glassfish.jersey.server.ResourceConfig;
public class MyApplication extends ResourceConfig {
public MyApplication(){
register(new APIBinder());
packages(true, "com.api.service");
}
}
我是从像这样的主要方法中调用它的
MyApplication app = new MyApplication();
错误是
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=ServiceProviderService,parent=ServiceProviderController,qualifiers={},position=-1,optional=false,self=false,unqualified=null,912348460)
请帮我解决这个问题,
非常感谢
ERO
添加了一个工厂实现,仍然没有得到修复同样的错误弹出
public class APIBinder extends AbstractBinder implements Factory<ServiceProviderService> {
@Override
protected void configure() {
// TODO Auto-generated method stub
bindFactory(this).to(ServiceProviderService.class);
}
public ServiceProviderService provide() {
ServiceProviderService sp= new ServiceProviderService();
return sp;
}
public void dispose(ServiceProviderService instance) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:-1)
您可以创建工厂。请参阅此主题以获取解释。
How do I configure HK2 to inject an object created by a factory method?