我不确定这是否行得通。所以:
选项1:使用注入提供程序SPI
实现一个提供程序,它将执行查找并注入EJB。看到:
@EJB注入。
com.sun.jersey:jersey-server:1.17的示例:import com.sun.jersey.core.spi.component.ComponentContext;import com.sun.jersey.core.spi.component.ComponentScope;import com.sun.jersey.spi.inject.Injectable;import com.sun.jersey.spi.inject.InjectableProvider;import javax.ejb.EJB;import javax.naming.Context;import javax.naming.InitialContext;import javax.ws.rs.ext.Provider;import java.lang.reflect.Type;@Providerpublic class EJBProvider implements InjectableProvider<EJB, Type> { public ComponentScope getScope() { return ComponentScope.Singleton; } public Injectable getInjectable(ComponentContext cc, EJB ejb, Type t) { if (!(t instanceof Class)) return null; try { Class c = (Class)t; Context ic = new InitialContext(); final Object o = ic.lookup(c.getName()); return new Injectable<Object>() { public Object getValue() { return o; } }; } catch (Exception e) { e.printStackTrace(); return null; } }}选项2:使BookResource成为EJB
@Stateless@Path("book")public class BookResource { @EJB private BookEJB bookEJB; //...}看到:
如何将REST服务与EJB 3.1结合
EJB 3.1和REST-轻量级混合
选项3:使用CDI
@Path("book")@RequestScopedpublic class BookResource { @Inject private BookEJB bookEJB; //...}


