WebServiceTemplate对象初始化一次后为null

WebServiceTemplate object is null after initializing once

我陷入了这个问题,不知道我做错了什么。 我正在使用 SOAPWS 并且当我调用该 web 服务时它给出了异常并且我检查了 WebServiceTemplate 对象是否为 null 但是一旦它在构造函数中初始化。 在它进入它设置为空的 sendNpsReminder 操作的那一刻。不知道为什么。有人可以帮忙吗?

Pom.Xml

        <dependency>
        <groupId>org.springframework.ws</groupId>
        <artifactId>spring-ws-core</artifactId>
        <version>2.1.2.RELEASE</version>
    </dependency>

Java类:

 public class ManageCustomerService extends WebServiceGatewaySupport {

    private static final CDLoggerInterface logger = CDLogger
            .getLogger(ManageCustomerService.class);

    private WebServiceTemplate manageContactsWSTemplate;



    public ManageCustomerService()
    {

    }
    public  ManageCustomerService(WebServiceTemplate manageContactsWSTemplateObj) {
        this.manageContactsWSTemplate = manageContactsWSTemplateObj;
            logger.debug("Constructor Setting the WebServieTemplte "+manageContactsWSTemplate);
    }



public SendNPSReminderResponseType sendNpsReminder()    {

    logger.debug("Calling now with new JOB="+manageContactsWSTemplate);

    SendNPSReminderRequestType sendNPSReminderRequest = new SendNPSReminderRequestType();
        Contact Contact = new Contact();                    
        Contact.setPhone(new BigInteger("XYZ"));  
        sendNPSReminderRequest.setCountryCode(ABC); 
        sendNPSReminderRequest.setContact(Contact);
        sendNPSReminderRequest.setCustomerNumber("23239220");

        if(manageContactsWSTemplate != null)
        {
            SendNPSReminderResponseType response = (SendNPSReminderResponseType)manageContactsWSTemplate.marshalSendAndReceive("http://tsi-vip-osbdev.canaldigital.com:7111//GSD_ManageCustomer_v2/ProxyService/Proxy/GSD_ManageCustomer_PS?wsdl", sendNPSReminderRequest, new SoapActionCallback("http://canaldigital.com/tsi/Schema/GSD/GSD_ManageCustomer/v2/GSD_ManageCustomer_SendNPSReminder"));  
            logger.info("Response for SendNpsReminder is : "+response.getResponseCode());
        }
        else{
            logger.debug("manageContactsWSTemplate is null");   
        }
    return xyz; 
}

ApplicationContext.xml

<oxm:jaxb2-marshaller id="manageContactMarshaller"
    contextPath="packagename" />
<oxm:jaxb2-marshaller id="manageContactUnmarshaller"
    contextPath="packagename" />

<bean name="manageContactsWSTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
    <constructor-arg ref="messageFactory" />
     <property name="defaultUri" value="http://tsi.com:7111/GSD_ManageCustomer_v1?WSDL" />  
    <property name="marshaller" ref="manageContactMarshaller" />
    <property name="unmarshaller" ref="manageContactUnmarshaller" />

</bean> 

<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">

</bean>
<bean id="manageContactService" class="com.canaldigital.tsi.managecontacts.serviceprovider.ManageCustomerService">

    <constructor-arg ref="manageContactsWSTemplate" />

</bean> 
<bean id="CDCommonTasksJob"
    class="com.canaldigital.tsi.managecontacts.serviceprovider.ManageCustomerService">
</bean>

<!-- Scheduler -->
<task:scheduled-tasks>
    <task:scheduled ref="CDCommonTasksJob" method="sendNpsReminder"
        cron="0 0/2 * * * *" />
</task:scheduled-tasks>

从您的 spring-context .xml 看来您正在请求相同 class :ManageCustomerService 的 2 个 bean,第一个将通过构造函数调用并将将有效的 spring bean 作为 WebServiceTemplate,但第二个 (CDCommonTasksJob) 将使用默认构造函数调用,因此对 WebServiceTemplate 的引用将为空。所以在你的任务调度器中,你自动装配没有 webServiceTemplate 实例的 bean

所以问题是,为什么要创建两个相同 class 的 bean?

如果您仍然需要两个 bean,只需替换行

<bean id="CDCommonTasksJob" class="com.canaldigital.tsi.managecontacts.serviceprovider.ManageCustomerService"> </bean>

与:

<bean id="CDCommonTasksJob" class="com.canaldigital.tsi.managecontacts.serviceprovider.ManageCustomerService"> <constructor-arg ref="manageContactsWSTemplate" /> </bean>