我想在子资源中注入一个商务服务bean,该子资源在专用类中定义并由子资源定位器传递. 一些示例代码: 根资源 @RequestScoped@Path("service")public class MyResource { @Context ResourceContext resourceCont
一些示例代码:
>根资源
@RequestScoped
@Path("service")
public class MyResource {
@Context
ResourceContext resourceContext;
// Sub resource locator
@Path("subservice")
public MySubResource locateToSubResource () {
// I don't want to create it myself.
return resourceContext.getResource(MySubResource.class);
}
}
>相应的子资源
@RequestScoped
public class MySubResource {
// Note that businessBean itself consists of
// multiple ejbs that also need to be injected so that it can do its job!
@Inject
private BusinessBean businessBean;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String get () {
return businessBean.doStuff();
}
}
Jersey不会让CDI调用依赖项…请注意,资源是托管对象.否则甚至不可能在根资源中注入一个bean(here I’m pushing my other questions’ view count to get more opinions
