开发者

@EJB dependency Injection doesn't work in JBoss AS 6 NullPointerException

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 says:

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

so far so good. But any attempt to modelClient results with "NullPointerException".

Any Idea ?

UPDATE: ModelServiceBean is looked up over JNDI (InitialContext) from a non-managed context

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

The exception stack trace:

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


How do you get hold to an instance of ModelServiceBean? Injection only works when ModelServiceBean itself is injected into some managed bean (e.g. a Servlet, JSF Managed bean, etc) or when you request it via JNDI.

Injection will not happen when you instantiate the class with the new operator.


We use @LocalBean rather than @Local on the implementation. I also see @Local on interfaces that are not remote.

Just a guess but I think that this might work:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜