7 Replies Latest reply on Jun 6, 2009 11:48 PM by Pete Muir

    Servlets not receiving injections

    John Ament Master

      So I created a simple servlet within my app that does a bit of proxying, to give nicer URLs for images that are requested. 


      I noticed I was getting a NPE.  When I looked, I found the object I was injecting was resolving to null.


      My servlet starts off like this..



      public class ImageProxyServlet extends HttpServlet{
           @Current ImagePathBuilder imagePathBuilder;
           @Initializer
           public ImageProxyServlet() {
                super();
                // TODO Auto-generated constructor stub
           }



      And the targetted bean:



      @Named("imagePathBuilder")
      public class ImagePathBuilder {



      I've tried the bean w/ a few different scopes (RequestScope, SessionScope) and nothing resolves.

        • 1. Re: Servlets not receiving injections
          Gavin King Master

          AFAIK, injection into servlets is not yet supported in the RI.

          • 2. Re: Servlets not receiving injections
            John Ament Master

            That seems a little confusing, as the PREVIEW 1.0 docs have a servlet injection example.


            Chapter 13

            • 3. Re: Servlets not receiving injections
              Gavin King Master

              Well, I'm wrong then. Happens :-)

              • 4. Re: Servlets not receiving injections
                John Ament Master

                Not saying you're wrong, I'm more inclined to believe the docs are wrong.  I added direct manager lookup via JNDI and it's there, so I'm inclined to think that someone was going to add in servlet injection, but the version of WebBeans on JBAS5.1 isn't working.  So docs are maybe out of sync w/ 5.1?

                • 5. Re: Servlets not receiving injections
                  Vivian Steller Newbie

                  I think injection into servlets currently only works in Tomcat 6 (with the appropriate Listener configured in your context.xml file, see the docs for details).


                  Though, it's not implemented for any version of JBoss AS. See WBINT-5

                  • 6. Re: Servlets not receiving injections
                    he youlin Novice
                    you can see org.jboss.webbeans.environment.tomcat.WebBeansAnnotationProcessor
                    
                    public void processAnnotations(Object instance) throws IllegalAccessException, InvocationTargetException, NamingException
                       {
                          manager.injectNonContextualInstance(instance);
                       }
                    
                    
                    
                    and org.jboss.webbeans.environment.servlet.Listener
                    
                    
                    if (tomcat)
                          {
                             // Try pushing a Tomcat AnnotationProcessor into the servlet context
                             try
                             {
                                Class<?> clazz = Reflections.loadClass(WebBeansAnnotationProcessor.class.getName(), Object.class);
                                Object annotationProcessor = clazz.getConstructor(WebBeansManager.class).newInstance(manager);
                                sce.getServletContext().setAttribute(WebBeansAnnotationProcessor.class.getName(), annotationProcessor);
                             }
                             catch (Exception e) 
                             {
                                log.error("Unable to create Tomcat AnnotationProcessor. JSR-299 injection will not be available in Servlets, Filters etc.", e);
                             }
                          }
                    
                    
                    and org.jboss.webbeans.environment.tomcat.WebBeansLifecycleListener
                    
                     private AnnotationProcessor getProcessor()
                                {
                                   if (processor == null)
                                   {
                                      Object o = servletContext.getAttribute("org.jboss.webbeans.environment.tomcat.WebBeansAnnotationProcessor");
                                      if (o instanceof AnnotationProcessor)
                                      {
                                         processor = (AnnotationProcessor) o;
                                      }
                                      else
                                      {
                                         return DUMMY_PROCESSOR;
                                      }
                                   }
                                   return processor;
                                }
                    
                    
                    
                    
                    so injection into servlets only works in tomacat,and you must add <Listener className="org.jboss.webbeans.environment.tomcat.WebBeansLifecycleListener"/> to context.xml
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    


                    • 7. Re: Servlets not receiving injections
                      Pete Muir Master

                      Vivian Steller wrote on Jun 05, 2009 02:51:


                      I think injection into servlets currently only works in Tomcat 6 (with the appropriate Listener configured in your context.xml file, see the docs for details).

                      Though, it's not implemented for any version of JBoss AS. See WBINT-5


                      100% correct, JBoss 5 is still using some JBoss 4 legacy code in this area, and so we struggled to find a good way to make it work. There will probably have to be another JBoss release to have it work correctly :-(