依赖注入 – 泽西岛:如何将EJB注入子资源?

我想在子资源中注入一个商务服务bean,该子资源在专用类中定义并由子资源定位器传递.

一些示例代码

>根资源

@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

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...