@EJB依赖注入在JBoss AS 6 NullPointerException中不起作用

时间:2011-05-31 15:32:24

标签: java java-ee jboss ejb cdi

public interface ModelClient {
    public JSONObject _callModel(URL url);
}

@Stateless
@Local(ModelClient.class)
public class ModelClientBean implements ModelClient {
    public JSONObject _callModel(URL url) { /* some implementation */ }
}

@Stateless(name="ejb/ModelServiceBean")
public class ModelServiceBean {

   @EJB
   private ModelClient modelClient;
}

JBoss AS 6说:

17:25:30,186 INFO  [org.jboss.ejb3.session.SessionSpecContainer] Starting jboss.j2ee:ear=rms-ear.ear,jar=rms-ejb-1.0-SNAPSHOT.jar,name=ModelClientBean,service=EJB3
17:25:30,187 INFO  [org.jboss.ejb3.EJBContainer] STARTED EJB: com.domain.rms.ejbs.ModelClientBean ejbName: ModelClientBean
17:25:30,250 INFO  [org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
rms-ear/rms-ejb-1.0-SNAPSHOT.jar/ModelClientBean!local - EJB3.x Default Local Business Interface
rms-ear/rms-ejb-1.0-SNAPSHOT.jar/ModelClientBean!com.domain.rms.ejbs.ModelClient - EJB3.x Local Business Interface
到目前为止一切顺利。 但是任何使用“NullPointerException”的modelClient结果的尝试。

任何想法?

UPDATE:从非托管上下文中查找JNDI(InitialContext)的ModelServiceBean

public JobServiceCall() {
    Context context = null;
    try {
        context = new InitialContext();
        modelServiceBean = (ModelServiceBean)context.lookup("..");
    } catch ()...
 }

异常堆栈跟踪:

Caused by: java.lang.NullPointerException
    at com.domain.rms.ejbs.ModelClientBean._callModel(ModelClientBean.java:53) [:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_24]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_24]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_24]
    at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_24]
    at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:122) [jboss-aop.jar:2.2.1.GA]
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:111) [jboss-aop.jar:2.2.1.GA]
    at org.jboss.ejb3.interceptors.container.ContainerMethodInvocationWrapper.invokeNext(ContainerMethodInvocationWrapper.java:72) [:1.1.3]
    at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.invoke(InterceptorSequencer.java:76) [:1.1.3]
    at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.aroundInvoke(InterceptorSequencer.java:62) [:1.1.3]
    at sun.reflect.GeneratedMethodAccessor279.invoke(Unknown Source) [:1.6.0_24]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_24]
    at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_24]
    at org.jboss.aop.advice.PerJoinpointAdvice.invoke(PerJoinpointAdvice.java:174) [jboss-aop.jar:2.2.1.GA]
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.GA]
    at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.fillMethod(InvocationContextInterceptor.java:74) [:1.1.3]
    at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_fillMethod_11016588.invoke(InvocationContextInterceptor_z_fillMethod_11016588.java)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.GA]
    at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.setup(InvocationContextInterceptor.java:90) [:1.1.3]
    at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_setup_11016588.invoke(InvocationContextInterceptor_z_setup_11016588.java)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.GA]
    at org.jboss.ejb3.async.impl.interceptor.AsynchronousServerInterceptor.invoke(AsynchronousServerInterceptor.java:128) [:1.7.17]
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.GA]
    at org.jboss.ejb3.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:62) [:1.7.17]
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.GA]
    at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:56) [:1.7.17]
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.GA]
    at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47) [:1.7.17]
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.GA]
    at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42) [:1.0.3]
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.GA]
    at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:68) [:1.7.17]
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.GA]
    at org.jboss.ejb3.core.context.SessionInvocationContextAdapter.proceed(SessionInvocationContextAdapter.java:95) [:1.7.17]
    at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.invokeInCallerTx(CMTTxInterceptor.java:223) [:0.0.1]
    ... 109 more

2 个答案:

答案 0 :(得分:2)

如何获得ModelServiceBean的实例?注入仅在将ModelServiceBean本身注入某个托管bean(例如Servlet,JSF托管bean等)或通过JNDI请求时才有效。

使用new运算符实例化类时不会发生注入。

答案 1 :(得分:0)

我们在实现上使用@LocalBean而不是@Local。我也在不远程的接口上看到@Local。

只是一个猜测,但我认为这可能有用:

@Local
public interface ModelClient {
    public JSONObject _callModel(URL url);
}

@Stateless
@LocalBean
public class ModelClientBean implements ModelClient {
    public JSONObject _callModel(URL url) { /* some implementation */ }
}