我正在尝试将 EJB 3.0 bean 注入(inject)到运行在 WebLogic 10.3.6 上的 Jersey 1.9 Servet。
我已经尝试过此处列出的技术:Inject an EJB into JAX-RS (RESTful service)
这里的直接注入(inject)技术只是给出一个NullPointerException
。 @Provider
技术给出了一个 NameNotFoundException
,因为它似乎正在提取本地接口(interface)的完全限定名称。将代码更改为仅使用接口(interface)名称似乎没有帮助。
我在 EAR 中打包。 EJB 在 JAR 中,Jersey 资源在 WAR 中。
EJB 注入(inject)到 WebLogic 10.3.6 上 Java EE 5 的 Jersey 中甚至可能吗?
最佳答案
显然 WebLogic 10.3.6 没有将本地业务接口(interface)注入(inject) 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)名称匹配很重要,因为下面的代码会获取该名称以允许注入(inject)。
下面的代码是根据上面的链接修改的,以获得 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;
}
}
}
关于jersey - 在 WebLogic 10.3.6 上将 EJB 3.0 注入(inject) Jersey 1.9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20522599/