JBoss Community

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?

created by Adheep M in EJB3 - View the full discussion

Viggo Navarsete wrote:

 

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

Start a new discussion in EJB3 at Community