顯然,WebLogic 10.3.6並未將本地業務接口注入到JNDI註冊中心。
根據Oracle Support說明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;
}
}
}