【问题标题】:Inject EJB 3.0 into Jersey 1.9 on WebLogic 10.3.6在 WebLogic 10.3.6 上将 EJB 3.0 注入 Jersey 1.9
【发布时间】:2013-12-29 15:07:43
【问题描述】:
我正在尝试将 EJB 3.0 bean 注入到在 WebLogic 10.3.6 上运行的 Jersey 1.9 Servet 中。
我已经尝试过这里列出的技术:Inject an EJB into JAX-RS (RESTful service)
这里的直接注入技术只是简单地给出一个NullPointerException。 @Provider 技术给出了NameNotFoundException,因为它似乎提取了本地接口的完全限定名称。更改代码以仅使用接口名称似乎没有帮助。
我在 EAR 中进行包装。 EJB 在 JAR 中,Jersey Resources 在 WAR 中。
EJB 在 WebLogic 10.3.6 上的 Java EE 5 上注入 Jersey 是否可能?
【问题讨论】:
标签:
jersey
weblogic
ejb-3.0
code-injection
【解决方案1】:
显然,WebLogic 10.3.6 没有将本地业务接口注入 JNDI 注册表。
根据 Oracle 支持说明 1175123.1,必须将 ejb-local-ref 添加到 web.xml:
<ejb-local-ref>
<ejb-ref-name>[Name of EJB local interface here]</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>[Fully qualified path to EJB local interface]</local>
</ejb-local-ref>
ejb-ref-name 与接口名称匹配很重要,因为这是通过下面的代码获得的以允许注入。
下面的代码是从上面的链接修改的,以获得Interface的简单名称,前缀为java:comp/env/以符合WebLogic 10.3.6命名标准。
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 java.lang.reflect.Type;
import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.ws.rs.ext.Provider;
/**
* JAX-RS EJB Injection provider.
*/
@Provider
public 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();
String simpleName = String.format("java:comp/env/%s", c.getSimpleName());
final Object o = ic.lookup(simpleName);
return new Injectable<Object>() {
public Object getValue() {
return o;
}
};
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}