gpt4 book ai didi

java - 了解 EJB 中无状态企业 session Bean 的功能

转载 作者:行者123 更新时间:2023-12-02 03:02:44 25 4
gpt4 key购买 nike

我是 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 依赖注入(inject)的类。

@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");"

任何帮助将不胜感激。

最佳答案

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

引用:http://docs.oracle.com/cd/E13222_01/wls/docs81/ejb/session.html

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

关于java - 了解 EJB 中无状态企业 session Bean 的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42212563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com