5 Replies Latest reply on Sep 11, 2008 11:13 AM by jaikiran pai

    DI and annotations not working

    Irina Connelly Newbie

      I am very new to JBoss and I cannot seem to resolve this issue.

      I am deploying a simple application in an ear file. The ear file contains a jar file with my EJBs and a war file with my web app files.

      The structure of the ear archive is as follows:

      MyApp.ear
       - my_ejb.jar
       - my classes
       - META-INF
       - persistence.xml
       - my_app.war
       - index.html
       - META-INF
       - WEB-INF
       - web.xml
       - META-INF
       - aplication.xml
       - jboss-app.xml
       - postgres-ds.xml
      



      I've exposed one of the session beans as a web service that I access from the web files. The problem I'm running into is that it seems like all DI related annotations are being ignored i.e. @EJB annotated items are null, @PostConstruct method is not getting called ...

      I've tried this in JBoss-5.0.0 and JBoss 4.2.2 and I'm getting the same behavior. JNDI lookup of the items works however.

      Please point me to something that I may be overlooking or am doing wrong.

      Thank you,
      Irina

        • 1. Re: DI and annotations not working
          jaikiran pai Master

          Please post the code where you are injecting the resources. Is it a EJB in which you are injecting the objects? If yes, how do you get hold of the EJB and invoke the EJB in your client? Post that code too.

          • 2. Re: DI and annotations not working
            Irina Connelly Newbie

            Here is the code for the java side

            
            @Stateless
            @WebService(serviceName="TestService")
            @SOAPBinding(
             style=javax.jws.soap.SOAPBinding.Style.RPC,
             use=javax.jws.soap.SOAPBinding.Use.LITERAL)
            public class TestWebService extends Remote implements TestWebServiceInterface {
            
             @EJB private DomainManager domainManager;
            
             public TestWebService() {
             }
            
             @WebMethod
             public String sayHello() {
            
             String hellostring = domainManager.sayHello();
             return hellostring;
             }
            }
            
            @Local
            public interface DomainManager {
            
             public String sayHello();
            }
            
            @Stateless
            public class DomainManagerBean implements DomainManager{
            
            
             public DomainManagerBean() {
             }
            
             public String sayHello() {
             return "Hello";
             }
            }
            
            


            I am invoking the web service from a flex application, not sure if pasting that code will help ...

            Upon invokation this is the line where the null pointer is thrown

            String hellostring = domainManager.sayHello();
            



            Alternatively, this is the code i was using inside the WebMethod to look up the bean

            
            InitialContext ctx = new InitialContext();
             domainManager = (DomainManager) ctx.lookup("RPortal/DomainManagerBean/local");
            
            


            and it works ...



            • 3. Re: DI and annotations not working
              jaikiran pai Master

               

              I am invoking the web service from a flex application, not sure if pasting that code will help ...


              The code would help. I am mainly interested in seeing how you are getting hold of the TestWebService bean in the code.



              • 4. Re: DI and annotations not working
                Irina Connelly Newbie

                The client side code needed to invoke the web service (TestService.as and others) were generated from a wsdl url using tools in Flex Builder 3.

                I am invoking the web service from my main application mxml file

                
                /**
                 * TestService.as
                 * This file was auto-generated from WSDL by the Apache Axis2 generator modified by Adobe
                 * Any change made to this file will be overwritten when the code is re-generated.
                 */
                package RPortal.atl{
                 import mx.rpc.AsyncToken;
                 import flash.events.EventDispatcher;
                 import mx.rpc.events.ResultEvent;
                 import mx.rpc.events.FaultEvent;
                 import flash.utils.ByteArray;
                 import mx.rpc.soap.types.*;
                
                 /**
                 * Dispatches when a call to the operation sayHello completes with success
                 * and returns some data
                 * @eventType SayHelloResultEvent
                 */
                 [Event(name="SayHello_result", type="RPortal.atl.SayHelloResultEvent")]
                
                 /**
                 * Dispatches when the operation that has been called fails. The fault event is common for all operations
                 * of the WSDL
                 * @eventType mx.rpc.events.FaultEvent
                 */
                 [Event(name="fault", type="mx.rpc.events.FaultEvent")]
                
                 public class TestService extends EventDispatcher implements ITestService
                 {
                 private var _baseService:BaseTestService;
                
                 /**
                 * Constructor for the facade; sets the destination and create a baseService instance
                 * @param The LCDS destination (if any) associated with the imported WSDL
                 */
                 public function TestService(destination:String=null,rootURL:String=null)
                 {
                 _baseService = new BaseTestService(destination,rootURL);
                 }
                
                 //stub functions for the sayHello operation
                
                
                 /**
                 * @see ITestService#sayHello()
                 */
                 public function sayHello():AsyncToken
                 {
                 var _internal_token:AsyncToken = _baseService.sayHello();
                 _internal_token.addEventListener("result",_sayHello_populate_results);
                 _internal_token.addEventListener("fault",throwFault);
                 return _internal_token;
                 }
                 /**
                 * @see ITestService#sayHello_send()
                 */
                 public function sayHello_send():AsyncToken
                 {
                 return sayHello();
                 }
                
                 /**
                 * @see ITestService#addsayHello()
                 */
                 public function addsayHelloEventListener(listener:Function):void
                 {
                 addEventListener(SayHelloResultEvent.SayHello_RESULT,listener);
                 }
                
                
                
                
                
                
                
                
                RPortal.mxml
                
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" borderStyle="none">
                 <mx:Script>
                 <![CDATA[
                 import RPortal.atl.*;
                
                 [Bindable]
                 private var testService:TestService = new TestService();
                
                 private function testWebService():void
                 {
                 // Attach the event handler
                 testService.addsayHelloEventListener(handleWebServiceResult);
                 // Invoke the service call
                 testService.sayHello();
                 }
                
                 private function handleWebServiceResult(event:SayHelloResultEvent):void
                 {
                 trace(event.result);
                 }
                 ]]>
                 </mx:Script>
                
                


                please let me know if this level of detail is enough or if I need to post anything else.

                I feel like I'm making a silly mistake somewhere in deployment but don't know where to look ...

                • 5. Re: DI and annotations not working
                  jaikiran pai Master

                  I'm not yet used to web services. But based on what i see, because of the way webservice TestWebService (which also is a EJB) is being constructed and invoked, the dependency injection never kicks in. My understanding is that the dependency injection will kick in only when the TestWebService is used as a EJB.

                  Someone with a better web services experience, here, might be able to help you understand the exact problem.