版权声明:来这世间走一遭总的留下点什么 https://blog.csdn.net/FightingITPanda/article/details/81252280
今天遇到个问题,还挺有意思,自己绕了半天没绕出去,跟同事探讨了一下,豁然开朗,team还是很重要啊,
代码是这样的:
这是一个调webservice服务的客户端,更新状态接口,
问题是这样的,外部业务逻辑执行完之后调getXML(String xml)这个方法把报文作为参数传过来,然后我把它赋给当前类属性xmlString ,然后new个对象调run()方法,在run()方法里把 xmlString作为参数传给接口,但是每次到run方法断点都拿不到 xmlString的值,为空,也就是没有给它赋值,而直接this.run()调用就好了,就能拿到值,这让我很费解,解答如下图,
package com.sinosoft.webservice.gsp;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class UpdateStateToVMS implements Runnable{
String xmlString = "" ;
public void getXML(String xml){
xmlString = xml;
//UpdateStateToVMS updateState = new UpdateStateToVMS();
// updateState.run();
this.run();
}
public void run(){
System.out.println(xmlString);
System.out.println("调用vms状态更新接口开始。。。");
Call call = null;
String endPoint = "http://localhost:8080/services/PandaService";
Service service = new Service();
try {
call = (Call)service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endPoint));
call.setOperationName("PandaMethod");
String res = (String)call.invoke(new Object[] { xmlString });
System.out.println("调接口返回信息。。"+res);
if(res.equals("TRUE")){
System.out.println("vms状态更新成功。。");
}else{
System.out.println("vms接口调用失败。。");
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("vms状态更新成功。。");
}
}
内存分配
外部调getXML方法时已经new了一个对象,分配内存情况如图左,左面updateState对象的xmlString属性是有值的,本类又new的对象分配内存情况如图右,直接去调run()方法,都没给XMLString赋值去run()方法里使用哪来的值。就是这样。。