4 Replies Latest reply: Mar 23, 2012 9:12 AM by Amber Jboss RSS

    Cannot inject field annotated with @Resource annotation in JBOSS 6

    Amber Jboss Newbie

      I am building a ear project using maven and embed a webservice war in it ;

       

        the testDataSource is the jndi name defined like this under JBOSS AS 6 deploy directory:

       

      <jndi-name>testDataSource</jndi-name>

       

       

      The web service endpoint is defined as:

       

      @Stateless()

       

       

      @TransactionManagement(TransactionManagementType.BEAN)

       

      @WebContext(contextRoot="/testMe")

       

      @WebService(name="TestDataSourceService",

      serviceName = "TestDataSourceServiceBeanService",

      portName = "TestDataSourceServicePort",

      targetNamespace = "http://testMe/")

       

      @SOAPBinding()

      public class TestSEIBean extends TestBaseServiceImpl  {

        @Resource(mappedName="testDataSource")

        protected DataSource dataSource;

       

      ....

      }

       

      the war(webservice) also has a dependency (so under WEB-INF/lib/testDataSource.jar with the same Resource annotation:


      that is class TestBaseServiceImpl  {

      @Resource(mappedName="testDataSource")

      protected DataSource dataSource;

       

      ...}

       

      My question is whether I can have the same @Resource annotation in my sei (service endpoint interface - web service) TestSEIBean  class, as well as in my TestBaseServiceImpl;

       

      When I deployed, I don't get exception and the jndi is bound. But when I use the generated stubs to call my web services, I would get this exception:

      :

      Cannot inject field annotated with @Resource annotation: protected javax.sql.DataSource TestSEIBean.dataSource

       

       

      Why can't Resource, in this case, jndi name be bound the the one I defined in xxxx-ds.xml under deploy directory?

      Thanks!

        • 1. Re: Cannot inject field annotated with @Resource annotation in JBOSS 6
          Amber Jboss Newbie

          ANY feedback is appreciated. I cannot imagine I am the only one who has a WebService on SLSB, and injected jndi @Resources for runtime lookup. I am also looking into Arquillian for integration testing, but I'd like to see if there is a simple fix here, as I struggle with the dependencies set up of arquillian in JBoss 6.

          • 2. Re: Cannot inject field annotated with @Resource annotation in JBOSS 6
            Amber Jboss Newbie

            I built a Arqullian test case using <arquillian.version>1.0.0.Alpha5</arquillian.version> and <jboss.version>6.0.0.Final</jboss.version>, and annotate a

               @Resource(mappedName = "java:testDataSource")

               protected DataSource dataSource;

             

            mvn -e  test -Pjbossas-managed-6

             

            assert not null on dataSource.getConnection() and the test passed.  I am lost as to why when I deployed on JBoss 6 and ran my test using the web services stub that it fails with :

             

            10:17:00,093 ERROR [org.jboss.wsf.common.injection.InjectionException] Resource 'testDataSource' not found: javax.naming.NameNotFoundException: testDataSource not bound

                    at org.jnp.server.NamingServer.getBinding(NamingServer.java:771) [:5.0.5.Final]

                    at org.jnp.server.NamingServer.getBinding(NamingServer.java:779) [:5.0.5.Final]

             

            when server starts up, the jndi DS is bound:

            10:16:19,865 INFO  [ConnectionFactoryBindingService] Bound ConnectionManager 'jboss.jca:service=DataSourceBinding,name=testDataSource' to JNDI name 'java:testDataSource'

             

            Can someone help, please?! What does it take to get a little help here?

            • 3. Re: Cannot inject field annotated with @Resource annotation in JBOSS 6
              Richard Opalka Master

              EJB3 is responsible for injection on SLSB endpoints.

              Anyway, can I see your full stack trace? I'm curious why

              jbossws-common InjectionHelper is in the stack trace.

              • 4. Re: Cannot inject field annotated with @Resource annotation in JBOSS 6
                Amber Jboss Newbie

                I think a lot of developers would probably appreciate the solution. This is simply solved by not using @Resource, instead you can use @Autowired, and/or with combination of @Qualifier, with the point being taking component scanning of @Resource out of the equation.