@EJB 依赖注入在 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 说:
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
到目前为止一切顺利。 但是任何对 modelClient 的尝试都会导致“NullPointerException”。
有什么想法吗?
更新:从非托管上下文通过 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如何获取 ModelServiceBean 的实例?仅当 ModelServiceBean 本身被注入到某些托管 bean(例如 Servlet、JSF 托管 bean 等)中或当您通过 JNDI 请求它时,注入才起作用。
当您使用 new 运算符实例化该类时,不会发生注入。
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.
我们在实现上使用@LocalBean而不是@Local。我还在非远程接口上看到@Local。
只是一个猜测,但我认为这可能有效:
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: