2017-02-13 113 views
0

我是EJB新手。我想了解無狀態企業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");" 

任何幫助設置的值,將不勝感激。

+0

因爲豆應該是** **無狀態的,即它不具有任何會話狀態。順便說一句,即使它有一個,你在flightService4上調用setAirplaneModel,然後在flightService上調用getAirplaneModel。 –

+0

@JBNizet我爲Typo道歉。請再次重訪這個問題。 –

+0

同樣,bean應該是**無狀態**。所以它不能有任何交談狀態。如果沒有兩個請求由同一個實例同時處理,容器可以爲任何請求重用任何實例。 –

回答