我們REST服務使用Jersy實現的,我的問題是調用我們的REST實現一些肥皂服務時,我們正在創造類似下面的委託對象,JAX-WS調用在Jersy
@POST
@Path("/forgotuserid/validate/mobilenumber")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public ServiceResponse validateMobileNumber(CommunicationDTO commonDTO)
throws ApplicationException, Exception {
ChMTYWebservicesProvidersWsMTY service = new ChMTYWebservicesProvidersWsMTY();
WsMTYPortType portType = service.getChMTYWebservicesProvidersWsMTYPort();
//TODO : other stuffs go here
return response;
}
is there any way to avoid new object creation and have single here?
如果'ChMTYWebservicesProvidersWsMTY'和'WsMTYPortType'是肥皂服務的存根(例如由axis生成),則可以將它們移動到util單例類。它將是線程安全的,因爲這個存根不包含狀態。 –
我實現了簡單的signleton模式來做到這一點,就像https://www.tutorialspoint.com/design_pattern/singleton_pattern.htm,但人們說它不是線程聖人 – sathishrtskumar
public static CrunchifySingleton getInstance(){ \t \t if(instance == null ){ \t \t \t //線程安全。可能是成本高的操作在一些情況下 \t \t \t同步(CrunchifySingleton.class){ \t \t \t \t如果(例如== NULL){ \t \t \t \t \t實例=新CrunchifySingleton(); \t \t \t \t} \t \t \t} \t \t} \t \t回報實例; \t}這可以嗎? – sathishrtskumar