Servlet中的EJB注入失败,“解决方案不应该通过注入容器发生”

时间:2011-03-07 23:22:30

标签: eclipse jboss ejb-3.0

我有一个带有一个ejb jar和web模块的EAR文件。 EJB jar包含一个无状态Ejb和本地接口

@Stateless(name="MsgProducer")
@Local(MsgProducerLocal.class)
public class MsgProducer implements MsgProducerLocal {
 public void sendMessage{}
}

现在我有一个servlet,我用@EJB注释注入这个会话bean但是当我在jboss 5.1中使用ejb jar和web.war模块部署这个EAR时,它会抛出以下异常。

java.lang.IllegalStateException: Resolution should not happen via injection container
        at org.jboss.web.tomcat.service.TomcatInjectionContainer.getEjbJndiName(TomcatInjectionContainer.java:640)
        at org.jboss.injection.EjbEncInjector.inject(EjbEncInjector.java:80)
        at org.jboss.web.tomcat.service.TomcatInjectionContainer.populateEnc(TomcatInjectionContainer.java:482)
        at org.jboss.web.tomcat.service.deployers.TomcatDeployment$EncListener.lifecycleEvent(TomcatDeployment.java:471)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4388)
        at org.jboss.web.tomcat.service.deployers.TomcatDeployment.performDeployInternal(TomcatDeployment.java:310)
        at org.jboss.web.tomcat.service.deployers.TomcatDeployment.performDeploy(TomcatDeployment.java:142)
        at org.jboss.web.deployers.AbstractWarDeployment.start(AbstractWarDeployment.java:461)
        at org.jboss.web.deployers.WebModule.startModule(WebModule.java:118)
        at org.jboss.web.deployers.WebModule.start(WebModule.java:97)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:157)
        at org.jboss.mx.server.Invocation.dispatch(Invocation.java:96)
        at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
        at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
        at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:668)
        at org.jboss.system.microcontainer.ServiceProxy.invoke(ServiceProxy.java:206)
        at $Proxy38.start(Unknown Source)

在我的web.xml中,我使用的是2.5版架构。这是我在servelet中的示例代码

public class MessageSenderServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @EJB(name="MsgProducer")
    public MsgProducerLocal producer;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public MessageSenderServlet() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        producer.sendMessage();
        response.sendRedirect("index.jsp");
    }

}

我正在使用Eclipse 3.6,JBoss 5.1和EJB 3.0。

有人能解释一下这段代码有什么问题吗? 如何解决此错误?

由于

1 个答案:

答案 0 :(得分:0)

您是否将Servlet放在.WAR文件中? 注意:您应该将您的Servlet放在.war文件中,将您的ejb放在jar文件中。然后创建一个ear文件。

如果您只实现一个接口,那么您不必使用@EJB(name =" MsgProducer") 。你可以简单地说@EJB public MsgProducerLocal producer;

正如您所提到的,您拥有web.xml版本2.5。请确保您使用的格式正确。

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">