2 Replies Latest reply on Oct 21, 2009 11:54 AM by Steve Bolan

    Best practice for injecting components from other EARs?

    Steve Bolan Newbie

      Hi,


      I've developed a Seam EAR project which uses Richfaces to provide administration functions to IBM Content Manager. 


      This project has an application-scoped component that I'd like to use in another project running on the same server.


      When I try to inject the component like this:


      @In ContentManagerServiceBean contentManagerService;


      contentManagerService ends up as null.


      What is the correct way to inject a component that is contained in another project?


      Many thanks,
      Steve

        • 1. Re: Best practice for injecting components from other EARs?
          Riccardo Serafin Newbie

          Unfortunately, you're a bit out of luck. Automatic injection for EJB works only with EJBs from same Ear (at least on JBoss, where the JNDI name uses a pattern that includes the ear name), as Seam does the lookup accordingly with the JNDI pattern, which necessarily either works for your own EJB or for an external EJB.


          The suggested pattern is to use the @Unwrap factory. Basically you have a component that does the lookup in the normal JNDI way and returns the EJB in a method annotated with @Unwrap. If you name such seam factory component with the same name you'd like to use for the injectintion, then something like @In ContentManagerServiceBean contentManagerService; would start to work.


          Your class would therefore be something like:




          @Name("contentManagerService")
          public class ContentManagerServiceFactory {
          
              ...
          
             @Unwrap
             public ContentManagerService serviceFactory(){
                 // do JNDI lookup
          
                 // Return the object obtained.
             }
          
          



          Of course you can put the lookup in an init method, and cache the way you prefer the reference to the remote EJB, scope it as you like etc.


          The other option is to configure the JNDI pattern of all EJBs (from your ear and from the external one) to use the same pattern and then to configure it in the seam app.


          Hope it helps,
          Riccardo.


          • 2. Re: Best practice for injecting components from other EARs?
            Steve Bolan Newbie

            Hi Riccardo,


            Thank you very much for your help - that worked perfectly :-)


            Cheers,
            Steve