EJB 3.0 - Injection via annotation into a servlet
neron17 Jan 26, 2010 9:45 AMHello Community,
I am trying to get a web application working. Therefore I made a servlet, a stateless session bean and an interface for that bean. Now I am trying to get my bean injected (I think that is the correct expression?) into the servlet to call a function. I activate the servlet via a url - that is working. But as soon as I enter the code for the injection my JBoss throws an error on deploying.
I have tried numerous ways but nothing helped. I have spent 3 days on this now and it would be really helpful if there is any advice for what I am doing wong.
Ok, lets get a bit more precise.
I am using Eclipse 3.5 (also to generate the EAR file for the JBoss) and JBoss 5.1.0.GA
My servlet code looks as follows:
package com; import java.io.IOException; import javax.ejb.EJB; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import com.session.PersonControllerEJBRemote; public class SnoopServlet extends HttpServlet { Logger logger = Logger.getLogger(SnoopServlet.class); @EJB PersonControllerEJBRemote personControllerEJB; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { logger.info("doGet()"); personControllerEJB.addPerson(); } }
Stateless Session Bean:
package com.session; import java.io.Serializable; import java.util.Date; import javax.ejb.Local; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.apache.log4j.Logger; import org.apache.ws.scout.registry.infomodel.PersonNameImpl; @Stateless public class PersonControllerEJBImpl implements PersonControllerEJBRemote, Serializable { @PersistenceContext(unitName="PersonControllerEJBImpl") private EntityManager em; private Logger logger = Logger.getLogger(PersonNameImpl.class); public void addPerson() { logger.info("addPerson()"); System.out.println("Person added!"); } public String findPerson(int personId) { return; } }
Interface
package com.session; import java.util.Date; import javax.ejb.Remote; @Remote public interface PersonControllerEJBRemote { public void addPerson(); public Person findPerson(int personId); }
and finally the error message:
15:22:06,375 INFO [TomcatDeployment] deploy, ctxPath=/tutorial 15:22:06,421 WARN [WebEJBRemoteHandler] EJBTHREE-1289: Using legacy EjbEncInjector, because mappedName for enc "env/com.SnoopServlet/personController EJB", field "null" is null (container.environmentRefGroup.annotatedEjbReferences = [AnnotatedEJBReferenceMetaData{name=com.SnoopServlet/personControll erEJB,ejb-ref-type=null,link=null,ignore-dependecy=false,mapped/jndi-name=null,resolved-jndi-name=null,beanInterface=interface com.session.PersonContr ollerEJBLocal}]) 15:22:06,437 ERROR [TomcatDeployment] ENC setup failed 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:585) 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) at org.jboss.system.microcontainer.StartStopLifecycleAction.installAction(StartStopLifecycleAction.java:42) at org.jboss.system.microcontainer.StartStopLifecycleAction.installAction(StartStopLifecycleAction.java:37) at org.jboss.dependency.plugins.action.SimpleControllerContextAction.simpleInstallAction(SimpleControllerContextAction.java:62) at org.jboss.dependency.plugins.action.AccessControllerContextAction.install(AccessControllerContextAction.java:71) at org.jboss.dependency.plugins.AbstractControllerContextActions.install(AbstractControllerContextActions.java:51) ...