Java org.glassfish.hk2.api.UnsatisfiedDependencyException: Injectee 没有可供注入的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21265285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee
提问by Dejell
I am new to Jersey 2. So far I worked with Jersey 1.x and Spring and would like to use HK2 implementation.
我是 Jersey 2 的新手。到目前为止,我使用 Jersey 1.x 和 Spring,并希望使用 HK2 实现。
After reading the tutorialI wrote the following:
阅读教程后,我写了以下内容:
@ManagedBean
@Path("products")
@Produces({ MediaType.APPLICATION_JSON })
public class ProductResource {
@Inject
ProductManager productManager;
@GET
public GenericResponseData<List<Product>> getProducts(@QueryParam("condition") Condition condition, @QueryParam("keywords") String keywords) {
GenericResponseData<List<Product>> res = new GenericResponseData<List<Product>>();
res.setObject(productManager.getProducts(condition, keywords));
return res;
}
}
@Contract
public interface ProductManager {
public List<Product> getProducts(Condition condition, String keywords);
}
@Service
public class MyProductManager implements ProductManager {
@Override
public List<Product> getProducts(Condition condition, String keywords) {
return null;
}
}
However I get the following exception:
但是我得到以下异常:
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee
What is wrong?
怎么了?
回答by Michal Gajdos
回答by jwells131313
I assume that the @Service annotation above is the hk2 @Service in which case you should know that @Service does not work automatically in Jersey. Instead you would need to add a binding that would do like a bind(MyProductManager).to(ProductManager) in some Jersey binder
我假设上面的@Service 注释是 hk2 @Service,在这种情况下,您应该知道 @Service 在 Jersey 中不会自动工作。相反,您需要在某些 Jersey 绑定器中添加一个类似于 bind(MyProductManager).to(ProductManager) 的绑定
回答by Boris
I was playing around with JAXRS and @Inject-ing EJB and got same error. With @EJB it worked fine.
我在玩 JAXRS 和 @Inject-ing EJB 并遇到同样的错误。使用@EJB,它运行良好。
The solution was to add CDI configuration file and change bean-discovery-mode="annotated" to bean-discovery-mode="all"
解决方案是添加 CDI 配置文件并将 bean-discovery-mode="annotated" 更改为 bean-discovery-mode="all"
After that I could use @Inject with my EJB.
之后,我可以将@Inject 与我的 EJB 一起使用。
This might help you also.
这也可能对您有所帮助。