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

前端之家收集整理的这篇文章主要介绍了依赖注入 – 泽西岛:如何将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

猜你在找的设计模式相关文章