2 Replies Latest reply on Feb 25, 2011 4:39 AM by stianst

    Inject custom resource with @Resource

    stianst

      Is it possible to inject a custom resource with @Resource? I'd like to be able to do:

       

      {code}

      @Resource

      private MyResource myResource

      {code}

       

      I would also like there to be multiple configurable versions of MyResource injected. For example:

       

      {code}

      @Resource(name="MyResource")

      private MyResource myResource

       

      @Resource(name="MyResource2")

      private MyResource myResource2

      {code}

        • 1. Re: Inject custom resource with @Resource
          jaikiran

          You can do that as long as MyResource is bound somewhere in the JNDI of the server. The injection into EJB is done by picking up the objects from the JNDI. So if your MyResource is available at "Blah" jndi-name for example, then you can do:

           

          @Resource (name = "someName", mappedName="Blah")
          private MyResource myResource;
          
          • 2. Re: Inject custom resource with @Resource
            stianst

            Thanks - got that working. What jndi-name would I have to bind it to if I don't want to specify the mappedName:

             

            {code}

            @Resource private MyResource myResource;

            {code}