Discussion:
[jboss-user] [EJB3] - How does a @EJB dependency injection work on a Stateless Bean? Will that injected class behave like an instance variable in that stateless session bean?
Adheep M
2012-06-22 05:14:59 UTC
Permalink
Adheep M [https://community.jboss.org/people/adheep] created the discussion

"How does a @EJB dependency injection work on a Stateless Bean? Will that injected class behave like an instance variable in that stateless session bean?"

To view the discussion, visit: https://community.jboss.org/message/743539#743539

--------------------------------------------------------------
Hi guys,

I have this question on the latest  version EJB 3.1 has the "@EJB" annotation which is used for the dependency injection.
I have two stateless session bean and a singleton bean

@Stateless
public class PrimaryStatelessBean {
 
     @EJB
     SecodaryStatelessBean secondaryStatelessBean;
 
     @EJB
     SimpleSingletonBean simpleSingletonBean;
 
     public String processRequest ( ) {
         
          .....
          secondaryStatelessBean.getValue();
          simpleSingletonBean.getAnotherValue();
          ....
     }
 
}

How does the @EJB annotation work, will the injected class behave like an Instance variable of the PrimaryStatelssBean ? :-/
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/743539#743539]

Start a new discussion in EJB3 at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2029]
Viggo Navarsete
2012-06-22 07:07:59 UTC
Permalink
Viggo Navarsete [https://community.jboss.org/people/viggo.navarsete] created the discussion

"Re: How does a @EJB dependency injection work on a Stateless Bean? Will that injected class behave like an instance variable in that stateless session bean?"

To view the discussion, visit: https://community.jboss.org/message/743572#743572

--------------------------------------------------------------
Yes, you're correct! Whenever a new instance of PrimaryStatelessBean is created by the EJB container (and since it's a stateless annotated bean, you will get a new one for each request), then you will get injected a new instance of SecondaryStatelessBean (since it's most likely also a stateless annotated bean, based on your naming convention), and you will get access to the *one instance* of SimpleSingletonBean that has been created by the EJB container (since it's a Singleton annotated bean).
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/743572#743572]

Start a new discussion in EJB3 at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2029]
Adheep M
2012-06-22 09:21:05 UTC
Permalink
Adheep M [https://community.jboss.org/people/adheep] created the discussion

"Re: How does a @EJB dependency injection work on a Stateless Bean? Will that injected class behave like an instance variable in that stateless session bean?"

To view the discussion, visit: https://community.jboss.org/message/743609#743609

--------------------------------------------------------------
@ https://community.jboss.org/people/viggo.navarsete Viggo Navarsete: Thanks for your reply.

But this brings me an another question, Supose if a method in "PrimaryStatelessBean" is annotated with REST will it work the same way, because we will not create a new instance for the PrimaryStatelessBean if it is invoked as a RESTful webservice.
@Stateless
public class PrimaryStatelessBean {

     @EJB
     SecodaryStatelessBean secondaryStatelessBean;

     @EJB
     SimpleSingletonBean simpleSingletonBean;
    
          @POST
          @Path("/requestService")
          @Consumes("*/*")
          @Produces("*/*")
     public String processRequest ( ) {
         
          .....
         
 
          secondaryStatelessBean.getValue();   //(After making a first request) When I make another request will i get the same value "Testing Value" here
 
          simpleSingletonBean.getAnotherValue();
 
 
 
 
          secondaryStatelessBean.setValue("Testing Value");   // set values for secondaryStatelessBean in the first request
          ....
 
         
 
 
     }
 
}

On the first request to my to "processRequest" service using REST, if i change the value of a member variable in SecondaryStatelessBean by

secondaryStatelessBean.setValue("Testing Value");   // set values for secondaryStatelessBean in the first request


and on the next request, will i get the value "Testing Value" here ?

secondaryStatelessBean.getValue();   //(After making a first request) When I make another request will i get the same value "Testing Value" here
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/743609#743609]

Start a new discussion in EJB3 at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2029]
Viggo Navarsete
2012-06-22 09:25:58 UTC
Permalink
Viggo Navarsete [https://community.jboss.org/people/viggo.navarsete] created the discussion

"Re: How does a @EJB dependency injection work on a Stateless Bean? Will that injected class behave like an instance variable in that stateless session bean?"

To view the discussion, visit: https://community.jboss.org/message/743610#743610

--------------------------------------------------------------
No, the "Testing Value" will not be there on the second request! But, it shouldn't be too difficult for you to actually test it, or? But I would assume that if you set a value on the simpleSingletonBean on the first request, then it would still be there on the second request!
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/743610#743610]

Start a new discussion in EJB3 at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2029]
Adheep M
2012-06-22 12:17:44 UTC
Permalink
Adheep M [https://community.jboss.org/people/adheep] created the discussion

"Re: How does a @EJB dependency injection work on a Stateless Bean? Will that injected class behave like an instance variable in that stateless session bean?"

To view the discussion, visit: https://community.jboss.org/message/743664#743664

--------------------------------------------------------------
Post by Viggo Navarsete
No, the "Testing Value" will not be there on the second request! But, it shouldn't be too difficult for you to actually test it, or? But I would assume that if you set a value on the simpleSingletonBean on the first request, then it would still be there on the second request!
I did try an example and below are the codes


package com.sample.rest.service;
 
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.test.ejb.PrimaryStatelessBean;
import com.test.ejb.SampleSingletonBean;
 
@Stateless
@Path("/services")
public class PrimaryStatelessBean{
 
     @EJB
    PrimaryStatelessBean primaryStatelessBean;
   
    @EJB
    SampleSingletonBean sampleSingletonBean;
 
    @GET
    @Path("/getValue")
    @Produces("text/xml")
    public String getValue(){
       
        //To get initial value
        System.out.println("Before Setting Value::" + primaryStatelessBean.getMessage());
 
        //set value for the Bean
        primaryStatelessBean.setMessage("Hi Buddy Cool!!");
       
        //After setting value
        System.out.println("After Setting Value::" + primaryStatelessBean.getMessage());
       
        return "<MESSAGE>"+primaryStatelessBean.getMessage()+"</MESSAGE><REQUEST_COUNT>"+ sampleSingletonBean.getCount()  +"</REQUEST_COUNT>";
 
    }
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/743664#743664]

Start a new discussion in EJB3 at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2029]
Darran Lofthouse
2013-01-17 10:58:26 UTC
Permalink
Darran Lofthouse [https://community.jboss.org/people/dlofthouse] created the discussion

"Re: How does a @EJB dependency injection work on a Stateless Bean? Will that injected class behave like an instance variable in that stateless session bean?"

To view the discussion, visit: https://community.jboss.org/message/792557#792557

--------------------------------------------------------------
Is there any thing that i'm missing here?
You never assign a new value to mRequestCount so it will always return 0+1. You are probably looking for ++mRequestCount.
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/792557#792557]

Start a new discussion in EJB3 at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2029]
Didac Montero Mendez
2013-01-17 16:23:41 UTC
Permalink
Didac Montero Mendez [https://community.jboss.org/people/umbreak] created the discussion

"Re: How does a @EJB dependency injection work on a Stateless Bean? Will that injected class behave like an instance variable in that stateless session bean?"

To view the discussion, visit: https://community.jboss.org/message/792647#792647

--------------------------------------------------------------
And in reference to the class SecondaryStatelessBean, this is an example of how a Stateless class should NOT be used.
Loading...