9 Replies Latest reply on Aug 4, 2015 10:11 AM by Prasanth Reddy

    Resource Injection in ServletContextListener

    Prasanth Reddy Newbie

      I have a ServletContextListener into which a DataSource has to be injected. It works fine in Jboss7.1 but does not work in 8.2 or 8.2.1 or 9.01. I have created a sample project to test it and seems like the injection is working if the class is a HttpServlet but doesn't work if the class is just a ServletContextListener. Am I missing something or is there a bug in wildfly?

       

      Thanks,

      Prasanth

       

      -----------------------jboss-web.xml-------------------------------

      <?xml version="1.0" encoding="UTF-8"?>

      <jboss-web>

          <context-root>sample</context-root>

          <virtual-host>sample</virtual-host>

         

          <resource-ref>

              <res-ref-name>DocServer</res-ref-name>

              <jndi-name>java:jboss/datasources/doc_server</jndi-name>

          </resource-ref>

      </jboss-web>

      -----------------------------------------------------------------------------

       

      public class ContextListener implements ServletContextListener {

         

          @Resource(name="DocServer")

          private DataSource dataSource;

       

          @Override

          public void contextDestroyed(ServletContextEvent arg0) {

              // TODO Auto-generated method stub

             

          }

       

          @Override

          public void contextInitialized(ServletContextEvent arg0) {

              // TODO Auto-generated method stub

             

          }

         

          public DataSource getDataSource() {

              return dataSource;

          }

       

      }