了解EJB中无状态企业会话bean的功能

时间:2017-02-13 20:03:29

标签: java servlets java-ee ejb stateless-session-bean

我是EJB新手。我试图了解无状态企业bean的功能。

看一下以下示例:

 @Stateless
 @LocalBean
 public class FlightService implements Serializable {

/**
 * Default constructor. 
 */
public FlightService() {
    // TODO Auto-generated constructor stub
}
/**
 * @return the id
 */
public Integer getId() {
    return id;
}
/**
 * @param id the id to set
 */
public void setId(Integer id) {
    this.id = id;
}
/**
 * @return the from
 */
public String getFrom() {
    return from;
}
/**
 * @param from the from to set
 */
public void setFrom(String from) {
    this.from = from;
}
/**
 * @return the to
 */
    public String getTo() {
        return to;
   }
   public void setTo(String to) {
    this.to = to;
    }
/**
 * @return the price
 */
   public Integer getPrice() {
    return price;
    }
/**
 * @param price the price to set
 */
    public void setPrice(Integer price) {
        this.price = price;
   }
/**
 * @return the numOfSeats
 */
    public Integer getNumOfSeats() {
        return numOfSeats;
   }
/**
 * @param numOfSeats the numOfSeats to set
 */
   public void setNumOfSeats(Integer numOfSeats) {
       this.numOfSeats = numOfSeats;
    }
  /**
   * @return the airplaneModel
 */
   public String getAirplaneModel() {
        return airplaneModel;
   }
/* (non-Javadoc)
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
        return "FlightService [id=" + id + ", from=" + from + ", to=" + to + ", price=" + price + ", numOfSeats="
            + numOfSeats + ", airplaneModel=" + airplaneModel + "]";
}
/**
 * @param airplaneModel the airplaneModel to set
 */
public void setAirplaneModel(String airplaneModel) {
    this.airplaneModel = airplaneModel;
}
private Integer id =23467;
private String from="Los Angles";
private String to="London";
private Integer price=400;
private Integer numOfSeats=250;
private String airplaneModel="Boeing 787";

}

这是使用@EJB依赖注入的类。

@EJB
private FlightService flightService;
@EJB
private FlightService flightService1;
@EJB
private FlightService flightService2;
@EJB
private FlightService flightService3;
@EJB
private FlightService flightService4;
@EJB
private FlightService flightService5;
@EJB
private FlightService flightService6;
public FlightDetails() {
    super();
    System.out.println(flightService);

}
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
  PrintWriter out = response.getWriter();
  response.setContentType("text/html");
  flightService.setAirplaneModel("Rishanth");
  flightService.setFrom("vijaywada");
  flightService.setId(1);
  flightService.setNumOfSeats(4);
  flightService.setPrice(4);
  flightService.setTo("hyderabad");      
  flightService4.setAirplaneModel("cannadian Airlines");
  out.println(flightService.getAirplaneModel());  

}

我想了解何时打印

 "flightService.getAirplaneModel()" 

为什么我在

上设置了值
 "flightService4.setAirplaneModel("cannadian Airlines");"

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

这主要是因为J2EE服务器将为每个@Statelss EJB提供一个池,并且对于每个单个客户端,将创建一个新实例。在您的情况下,恰好只有一个客户端正在使用注入的EJB。

参考:http://docs.oracle.com/cd/E13222_01/wls/docs81/ejb/session.html

类似的问题:Difference between @Stateless and @Singleton