1 2 Previous Next 26 Replies Latest reply on Jan 14, 2013 10:31 AM by nickarls

    Session bean injection

    ran_inc

      Hi,

      I am trying to inject a session bean into another session bean (in this preticular case its the same session bean), and i get this error:

      org.jboss.weld.exceptions.UnsatisfiedResolutionException: WELD-001308 Unable to resolve any beans for Types: [interface com.windriver.dsm.labmanagement.ejb.stub.GeneralSession]; Bindings: [@javax.enterprise.inject.New(value=com.windriver.dsm.labmanagement.ejb.stub.GeneralSession.class)]

       

      this is  how I am trying to do that:

       

       

      @Stateless

      @TransactionManagement(value=TransactionManagementType.CONTAINER)

      @TransactionAttribute(value=TransactionAttributeType.REQUIRED)

      @Local(GeneralSessionLocal.class)

      @Remote(GeneralSession.class)

      public class GeneralSessionBean extends CRUDSessionBase

      {

       

       

                @Inject @New

                Instance<GeneralSession> generalSessionInstance;

      ....

      }

       

      I get the error when I call generalSessionInstance.get();

       

      can someone help?

        • 1. Re: Session bean injection
          lafr

          As far as I know, this is not possible. Because of this I continued to go with EJB for EJB-injection.

           

          From http://www.adam-bien.com/roller/abien/entry/inject_vs_ejb:

          Sometimes it may be necessary to use a "self" reference to an EJB. You could inject a reference to "self" with @EJB, but not @Inject.

          • 2. Re: Session bean injection
            nickarls
            • 3. Re: Session bean injection
              ran_inc

              when I use @ejb - I get org.jboss.as.server.deployment.DeploymentUnitProcessingException: JBAS014544: No EJB found with interface of type 'javax.enterprise.inject.Instance' for binding com.windriver.dsm.labmanagement.ejb.session.GeneralSessionBean/generalSessionInstance.

              • 4. Re: Session bean injection
                sfcoy

                I think it would help to know exactly what you're trying to accomplish here.

                • 5. Re: Session bean injection
                  ran_inc

                  I want to inject a session bean to another session bean.

                  specificly I inject the self session bean in order to call another method in a different transaction attribute.

                  but anyways why does the inject fail?

                  • 6. Re: Session bean injection
                    sfcoy

                     

                    {code:java}

                    @Stateless

                    @Local(GeneralSessionLocal.class)

                    @Remote(GeneralSession.class)

                    public class GeneralSessionBean extends CRUDSessionBase

                    {

                     

                         @Resource

                         private SessionContext sessionContext;

                     

                         // There's no point in this being the remote interface

                         private GeneralSessionLocal self;

                     

                         @PostConstruct

                         void initialise() {

                              self = sessionContext.getBusinessObject(GeneralSessionLocal.class);

                         }

                     

                    }

                    {code}

                     

                     

                    This "trick" has been around at least ten years I think....

                    • 7. Re: Session bean injection
                      sfcoy

                      ran haim wrote:

                       

                      but anyways why does the inject fail?

                      It fails because CDI beans and EJBs have different life cycle management.

                      • 8. Re: Session bean injection
                        ran_inc

                        Thanks Stephen!

                         

                        I have a follow up questions (which might be considered to be one question):

                        1. If I want to initialize the session bean only In a sepecific method - this is why I used Instance<GeneralSession>.

                        2. Also How can I inject a different session bean (not self), and also make sure it's injected only when I use it (like Instance capabliblities)?

                        • 9. Re: Session bean injection
                          sfcoy

                          ran haim wrote:

                           

                          ...

                           

                          1. If I want to initialize the session bean only In a sepecific method - this is why I used Instance<GeneralSession>.

                          You can invoke

                          self = sessionContext.getBusinessObject(GeneralSessionLocal.class);

                          from any method you like. It doesn't have to be the way I did it.

                           

                          But if you do it my way it's guaranteed to be set up whenever you need it and the cost is tiny. There's no chance of your code generating NPEs this way. Always initialise variables as soon as you can.

                           

                           

                          2. Also How can I inject a different session bean (not self), and also make sure it's injected only when I use it (like Instance capabliblities)?

                          You can't inject it (using the APIs currently available). You need to perform a JNDI lookup to get EJB instances dynamically.

                           

                          Typically you would do this when using remote EJBs that may be unavailable for some reason.

                          • 10. Re: Session bean injection
                            ran_inc

                            I am pretty sure that @Inject can inject any managed bean and is a part of the new CDI specification (since Java EE 6) - which should include session beans.

                            Am I wrong?

                            • 11. Re: Session bean injection
                              nickarls

                              Can you do a plain @Inject @New (without Instance<>)?

                              • 12. Re: Session bean injection
                                sfcoy

                                In retrospect (caused by going back and reading the spec again) maybe this should work. Does your deployment contain a META-INF/beans.xml file?

                                 

                                Also, try injecting the local interface as I suspect that trying to inject a remote is being hopeful.

                                • 13. Re: Session bean injection
                                  lafr

                                  I can confirm that using self references works with @EJB for JBoss AS7.

                                  I use this in a number of beans.

                                   

                                  @Stateless

                                  public class MessageEraser extends EraserBase

                                  {

                                      @EJB

                                      private MessageEraser messageEraser;

                                   

                                      @Timeout

                                      public void ejbTimeout( final Timer timer ) {

                                          ...

                                          this.messageEraser.removeEntity( id );

                                      }

                                   

                                      @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

                                      public void removeEntity( final Integer key )

                                      {

                                          ...

                                      }

                                  }

                                   

                                  Before this, I had to use Context.getBusinessObject().

                                  • 14. Re: Session bean injection
                                    sfcoy

                                    I would have thought that this would recurse to death.

                                     

                                    Is this behaviour a happy accident or is it spec defined somewhere?

                                    1 2 Previous Next