1 2 Previous Next 22 Replies Latest reply on Apr 13, 2009 5:52 AM by sannegrinovero

    Why is injection not working in AS 4.0 and 4.2 (servlets)?

    wolfc

      There are a lot of questions about injection not working in AS 4.0 and 4.2. Most of the problems boil down to injection not working the web layer.

      This is because AS 4.0 and 4.2 are J2EE 1.4 servers with an EJB3 plugin. This means that injection and annotations only work within the EJB3 plugin. To get to your EJBs you still have to perform a JNDI lookup from your servlet / application client.

      To get all the functionality of a JavaEE 5 server you must use JBoss Application Server 5 instead.

        • 1. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
          wolfgangknauf

          One thing to add: Injection in a JSF mananged bean will work in JBoss 4.2

          I managed to achieve this only by accessing the ENC:

          @EJB(name="java:comp/env/ejb/MyBeanLocal")
          private MyBeanLocal myBean;


          I had to declare it in web.xml and jboss-web.xml.
          This is my jboss-web.xml:
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE jboss-web PUBLIC
           "-//JBoss//DTD Web Application 4.2//EN"
           "http://www.jboss.org/j2ee/dtd/jboss-web_4_2.dtd">
          <jboss-web>
           <context-root>MyWeb</context-root>
           <ejb-local-ref>
           <ejb-ref-name>ejb/MyBeanLocal</ejb-ref-name>
           <local-jndi-name>MyEAR/MyBean/local</local-jndi-name>
           </ejb-local-ref>
          </jboss-web>


          Simply declaring "@EJB" without attributes did not work. Maybe someone can clear this ?

          Wolfgang

          • 2. Re: Why is injection not working in AS 4.0 and 4.2 (servlets


            I see that this thread and its contents have been given "sticky" status, yet there seems to be little content in it.

            No offense to Wolfgang here, but is that what passes for good coverage of a problem like this? I mean, release 4.2 was touted as being "fully Java EE compliant" (I paraphrase) by the program manager (or some such figure), and yet something central to the Servlet 2.5 specification - supplied by the included Tomcat 6, I'm told - doesn't seem to work. I'm not complaining, really, I'm just mystified... Am I the only one trying this? Is there such little support out there that *no one* has an answer? Please, someone, give a little more insight on this problem with 4.2 GA...

            • 3. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
              jobor

              Hello,

              I wanted to use EJB3 with JSF1.2 and injection. For this combination you have to do a little bit more work in JBoss-4.2.0.GA. This is how injection worked for me:

              The ejb jar and war file are deployed separate from each other in the same deploy diectory. So not in an EAR file! There is also no ejb-jar.xml in the EJB3 app.

              The EJB3 bean:

              @Stateless
              @Local(NewsAgentLocal.class)
              public class NewsAgentBean implements NewsAgent {
              ...
              }
              


              The EJB3 local interface:
              @Local
              public interface NewsAgentLocal extends NewsAgent {
              }
              


              The JSF1.2 backing bean:
              public class WelcomeHandler {
              
               @EJB(name="java:comp/env/ejb/NewsAgent") private NewsAgentLocal agent;
              
               public DataModel getNewsModel() {
               NewsFilter f = new NewsFilter();
               Integer year = new Integer(Calendar.getInstance().get(Calendar.YEAR));
               f.setYear(year.shortValue());
               newsModel.setWrappedData(agent.getNewsItems(f));
               return newsModel;
               }
              ...
              }
              


              The web.xml:
               <ejb-local-ref>
               <ejb-ref-name>ejb/NewsAgent</ejb-ref-name>
               <ejb-ref-type>Session</ejb-ref-type>
               <local-home></local-home>
               <local>nl.borsoft.hd.news.NewsAgentLocal</local>
               </ejb-local-ref>
              


              The jboss-web.xml:
               <ejb-local-ref>
               <ejb-ref-name>ejb/NewsAgent</ejb-ref-name>
               <local-jndi-name>NewsAgentBean/local</local-jndi-name>
               </ejb-local-ref>
              


              For all XML descriptors I do use the EE5 document type declarations.
              You can find them in jboss-4.2.0.GA\docs\dtd and jboss-4.2.0.GA\docs\schema.

              With regards,
              Johan Borchers


              • 4. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
                jobor

                There is little trick using the injection and JSF1.2 backing beans.
                If you plan to use the bean during the constructor of the backing bean, that won't work.
                In my backing beans I do call e.g. a find() method in the backing bean from within the constructor.
                But in the old situation the first thing I did was looking up the 2.1 EJB so it was there when I needed it.
                So the injection comes in later than during the execution of the constructor of the backing bean.

                Old situation that does not work:

                public class CatalogHandler {
                
                 @EJB(name="java:comp/env/ejb/CatalogAgent") private CatalogAgentLocal agent;
                
                 public CatalogHandler() {
                 ...
                 find();
                 }
                
                 public DataModel getCatalogModel() {
                 return catalogModel;
                 }
                
                 public String find() {
                 catalogModel.setWrappedData(agent.getCatalogItems(filter));
                 ...
                 }
                
                }
                


                The new sitiuation with a little (not very nice) trick that does work:
                public class CatalogHandler {
                
                 @EJB(name="java:comp/env/ejb/CatalogAgent") private CatalogAgentLocal agent;
                
                 private boolean doInitLater = false;
                
                 public CatalogHandler() {
                 ...
                 doInitLater = true;
                 }
                
                 public DataModel getCatalogModel() {
                 if (doInitLater) {
                 doInitLater = false;
                 find();
                 }
                 return catalogModel;
                 }
                
                 public String find() {
                 catalogModel.setWrappedData(agent.getCatalogItems(filter));
                 ...
                 }
                
                }
                


                Johan Borchers

                • 5. Re: Why is injection not working in AS 4.0 and 4.2 (servlets

                  So JBoss 5.0 beta2 supports servlet injection? Or is the functionality only going to be fully delivered in the final release?

                  • 6. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
                    jainsau

                    i have also seen that, within the AS, in the same ear, EJB injections do not work. So if an EJB access another EJB using @EJB it works. But if the EJB access another class (simple POJO) which uses @EJB, the injection doesnt work (EJB->POJO->EJB does not work, though POJO is in the same EAR file and is invoked directly from the EJB).
                    Is this something missing in the Jboss 4.2 or is this expected behavior? Or do I need to do some configuration?
                    The EJB specs seem to be silent on this behavior.

                    • 7. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
                      ssilvert

                       

                      "tinny" wrote:
                      So JBoss 5.0 beta2 supports servlet injection? Or is the functionality only going to be fully delivered in the final release?


                      Yes and Yes. The only injection I've heard that doesn't currently work is @EJB in the web layer. But everything in the JEE5 spec will be delivered in the final release of JBoss 5.

                      Stan

                      • 8. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
                        jfheintz

                        Hello,

                        I agree the way you get the Home interface of a session bean, but I am not able to do the same for an entity.

                        There is no JNDI entry for the entities beans.

                        I can see only the persistence manager:

                        +- persistence.units:ear=myapp.ear,unitName=MyUnit (class: org.hibernate.impl.SessionFactoryImpl)

                        But no way to get the associated EntityManager.

                        Regards,

                        JF HEINTZ

                        • 9. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
                          jfheintz

                          I tried to set the EntityManager using it's factory, but I have a transaction exception about setting an autcomit

                          EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("MyUnit");
                          this.entityManager = entityManagerFactory.createEntityManager();

                          Any idea?

                          Regards

                          JF HEINTZ

                          • 10. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
                            alrubinger
                            • 11. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
                              rbellia

                              I´m trying to do JPA Injection in a WAR module with JBoss 5 beta2.

                              I´m configuring the persistence.xml according to
                              http://docs.jboss.org/ejb3/app-server/reference/build/reference/en/html/entityconfig.html

                              Here is my persistence.xml:

                              <persistence version="1.0"
                              xmlns="http://java.sun.com/xml/ns/persistence"
                              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                              xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                              http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
                               <persistence-unit name="TesteJPA" transaction-type="JTA">
                               <jta-data-source>java:jdbc/testeJPA</jta-data-source>
                               <properties>
                               <property name="jboss.entity.manager.jndi.name" value="java:jpa/TesteJPA"/>
                               <property name="jboss.entity.manager.factory.jndi.name" value="java:jpa/TesteJPAFactory"/>
                               </properties>
                               </persistence-unit>
                              </persistence>


                              In JBoss 5 beta 2 the persistence unit is only considered automatically when persistence.xml is placed in WEB-INF folder. It differs froms JPA spec chapter 6.2, where says it should be in WEB-INF/classes/META-INF.

                              Everything stars fine with Hibernate EntityManager, until the following error shows up:

                              06:13:23,625 INFO [SessionFactoryObjectFactory] Factory name: persistence.units:unitName=TesteJPA
                              06:13:23,765 INFO [NamingHelper] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingC
                              ontextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
                              06:13:24,906 INFO [SessionFactoryObjectFactory] Bound factory to JNDI name: persistence.units:unitName=TesteJPA
                              06:13:25,000 WARN [SessionFactoryObjectFactory] InitialContext did not implement EventContext
                              06:13:25,000 INFO [NamingHelper] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingC
                              ontextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
                              06:13:37,625 INFO [TomcatDeployment] deploy, ctxPath=/TesteWebApplicationJPAJBoss, vfsUrl=JBossWebApplicationJPA.war
                              06:13:37,875 ERROR [BaseModelMBean] Exception invoking method addChild
                              java.lang.RuntimeException: persistence.units:unitName=java:jpa/TesteJPAFactory
                              at org.jboss.ejb3.JmxDependencyPolicy.addDependency(JmxDependencyPolicy.java:48)
                              at org.jboss.injection.PersistenceUnitHandler.addPUDependency(PersistenceUnitHandler.java:131)
                              at org.jboss.injection.PersistenceUnitHandler.handleFieldAnnotations(PersistenceUnitHandler.java:243)
                              ...
                              Caused by: javax.management.MalformedObjectNameException: Invalid character ':' in value part of property
                              at javax.management.ObjectName.construct(ObjectName.java:529)
                              at javax.management.ObjectName.(ObjectName.java:1304)
                              at org.jboss.ejb3.JmxDependencyPolicy.addDependency(JmxDependencyPolicy.java:44)
                              ... 78 more
                              06:13:37,953 ERROR [AbstractKernelController] Error installing to Start: name=jboss.web.deployment:war=/TesteWebApplicat
                              ionJPAJBoss state=Create mode=Manual requiredState=Installed
                              LifecycleException: Pipeline has not been started
                              at org.apache.catalina.core.StandardPipeline.stop(StandardPipeline.java:257)
                              at org.apache.catalina.core.StandardContext.stop(StandardContext.java:4541)
                              at org.apache.catalina.core.ContainerBase.destroy(ContainerBase.java:1134)
                              at org.apache.catalina.core.StandardContext.destroy(StandardContext.java:4617)
                              at org.apache.catalina.core.StandardContext.init(StandardContext.java:5315)
                              ...

                              Am I missing something or JBoss 5 beta2 stills not suporting JPA Injection ?


                              • 12. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
                                rbellia

                                JBoss 4.2.1 doen´t seem to support JPA Injection in servlets either.

                                Trying the same WAR i have tryed with 5 beta2, I´m facing the following problems:

                                The Persistence Unit is not considered automatically.
                                I had to manually start the EMFactory up from a Filter.init or some ServletContextListener, loading persistence.xml from WEB-INF/classes/META-INF.

                                The manual startutp complains some a lack of access to Transaction Managers, so I had to add the following properties in persistece.xml:

                                <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
                                <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
                                <property name="jboss.entity.manager.jndi.name" value="java:jpa/TesteJPA"/>
                                <property name="jboss.entity.manager.factory.jndi.name" value="java:jpa/TesteJPAFactory"/>
                                



                                Anyway the JNDI Tree doens´t show any new entries for my JPA Componentes

                                The Servlet (see code below) is neither receiveing JPA Injection nor UserTransaction Injection.

                                public class ServletJPA extends HttpServlet {
                                
                                 @PersistenceUnit(unitName="java:jpa/TesteJPAFactory") EntityManagerFactory emf;
                                 @Resource(name="java:UserTransaction") UserTransaction ut;
                                
                                 ... doGet (...) {
                                 doJPA();
                                 }
                                
                                 private String doJPA() throws NotSupportedException, SystemException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
                                
                                 EntityManager em = null;
                                 try {
                                 getServletContext().log("emf == null ? " + (emf == null)); // true !
                                 getServletContext().log("ut == null ? " + (emf == null)); // true !
                                 ut.begin();
                                 em = emf.createEntityManager();
                                 MyEntity x = new MyEntity(...);
                                 em.persist(x);
                                 ut.commit();
                                 return "new ID :" + x.getId();
                                 } finally {
                                 if (em != null) em.close();
                                 }
                                
                                 }
                                
                                



                                I see comments in this thread saying that only @EJB infection is missing...
                                Is there something I´m missing ?

                                • 13. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
                                  awssul

                                  I have noticed and also mentioned in this forum then @EJB does not work in versions prior to jboss 5. So can any when tell what is the alternate ejb3 injection annotation for jboss. Will @Resource work, if yes then how?

                                  • 14. Re: Why is injection not working in AS 4.0 and 4.2 (servlets
                                    dushyant01

                                    Hi,
                                    My application consists of the a JSF Page that has a backing bean- view.backing.Start.

                                    here i'm trying to lookup my ejb using
                                    @EJB(mappedName=...jndiName)
                                    private JBejb obj;

                                    where JBejb is the remote interface of my SLSB-JBejbBean.

                                    i am facing the problem that my JBoss4.2.1 server is giving the following exception -
                                    javax.naming.NameNotFoundException:view.backing.Start is not bound.

                                    I don't Understand why does the server want the backing bean of the JSF page

                                    1 2 Previous Next