我有一个名为MyService.java的简单POJO类:
@RequestScoped
public class MyService {
public String helloWorld(){
return "hello world!";
}
}
我有一个JAX-RS类,我想用CDI注入我的POJ:
@Path("service")
public class RestfulService {
public static AtomicInteger counter = new AtomicInteger(0);
@EJB
MyEJB ejb;// The EJb injection works!
@Inject
MyService service; // CDI injection does NOT work!
@GET
@Path("counter")
@Produces(MediaType.TEXT_PLAIN)
public String getTotal() {
return "Number of hits: " + counter.incrementAndGet();
}
}
以下是抛出的异常:
g.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=MyService,parent=RestfulService,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1492861225)
at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74)
文件beans.xml
存在且发现模式设置为annotated
。
请帮忙吗?对我来说似乎是一个基本的例子。