2016-11-16 15 views
1

我們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? 
+0

如果'ChMTYWebservicesProvidersWsMTY'和'WsMTYPortType'是肥皂服務的存根(例如由axis生成),則可以將它們移動到util單例類。它將是線程安全的,因爲這個存根不包含狀態。 –

+0

我實現了簡單的signleton模式來做到這一點,就像https://www.tutorialspoint.com/design_pattern/singleton_pattern.htm,但人們說它不是線程聖人 – sathishrtskumar

+0

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

回答

0

如果您正在使用Spring框架然後有一個選項依賴注入,您可以使用該功能。

+0

由於我們不使用Spring框架,不能擁有單例對象。 – sathishrtskumar

+0

創建單例類; –

+0

@sathishrtskumar public static Test test = new Test();並使用ClassName.test –

0

您可以像這樣代碼的東西:

public class SoapWSUtil{ 
    private static WsMTYPortType type; 

    static { 
     type = (new ChMTYWebservicesProvidersWsMTY()).getChMTYWebservicesProvidersWsMTYPort(); 
    } 

    public static WsMTYPortType getType(){ 
     return type; 
    } 
} 

,然後用它作爲SoapWSUtil.getType()。如果你不會添加狀態到SoapWSUtil

+0

謝謝Krauchanka – sathishrtskumar