繁体   English   中英

自动连线的Spring NullPointerException

[英]Autowired Spring NullPointerException

我的代码尝试生成@Autowired时遇到问题。

班级:

public class ConsultasMDMWSClientImpl implements ConsultasMDMWSClient {

    @Autowired
    ConsultasMDMWSPortype consultasMDMWSPortype;
    public ConsultarClienteResponseMDM consultarClienteEnMdm(ConsultarClienteRequest clienteReq) {

       ConsultarClienteResponseMDM response = new ConsultarClienteResponseMDM();
       ConsultasMDMWSService consultasMDMWSService = new ConsultasMDMWSService();

       ConsultarClienteResponse clienteResp = null;
       clienteResp = consultasMDMWSPortype.consultarCliente(clienteReq);
       ListaCursoresMDM listaCursores;
            listaCursores = new ObjectMapper().readValue(clienteResp.getListaCursoresResponse(), ListaCursoresMDM.class);

       response.getListaCursoresResponse().add(listaCursores);
    return response;
    }
 }

我的applicationContext.xml

      <context:annotation-config/>
      <context:component-scan base-package="pe.com.claro.eai.esb.ws.jira.mdm"/>
      <import resource="wsclients-config.xml"/>

我的wsclients-config.xml

 <bean id="consultasMDMWSPortype" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
           <property name="serviceInterface" value="pe.com.claro.eai.consultasmdmws.ConsultasMDMWSPortype"/>    
           <property name="wsdlDocumentUrl" value="http://limdeseaiv28.tim.com.pe:8909/ConsultasMDMWS/ConsultasMDMPortSB11?wsdl"/>
           <property name="namespaceUri" value="http://eai.claro.com.pe/ConsultasMDMWS"/>
           <property name="serviceName" value="ConsultasMDMWSService"/>
           <property name="portName" value="ConsultasMDMPortSB11"/>
           <property name="lookupServiceOnStartup" value="false"/>
      </bean>

        <bean id="consultasMDMWSClient"
            class="pe.com.claro.eai.esb.ws.jira.mdm.service.client.ConsultasMDMWSClientImpl">
                <property name="consultasMDMWSPortype" ref="consultasMDMWSPortype"/>
       </bean> 

我不知道自己在做什么错,我已经映射了所有内容,例如我在Spring上刚接触到的工作的示例,而Web方法在没有Spring的情况下可以工作。

该错误仅在我使用@Autowired时出现。

java.lang.NullPointerException

解冻所有人。

作为@Christopher提出的解决方案的替代方法,如果您要保留“旧式” XML配置注入(setter注入),则需要删除@Autowired批注并为ConsultasMDMWSPortype声明一个setter,即:

ConsultasMDMWSPortype consultasMDMWSPortype;

public ConsultasMDMWSPortype setConsultasMDMWSPortype(ConsultasMDMWSPortype consultasMDMWSPortype) {
    this.consultasMDMWSPortype = consultasMDMWSPortype;
}

因此,spring将能够通过setter方法连接xml中配置的ref-bean。

您可以尝试在ConsultasMDMWSClientImpl类的顶部添加@Component批注。

喜欢:

@Component
public class ConsultasMDMWSClientImpl implements ConsultasMDMWSClient {

需要指出这是一个弹簧豆,以便弹簧容器在启动弹簧容器时对其进行扫描并初始化为弹簧豆。

希望对您有所帮助。

如前所述,您正在将XML连接与注释连接混合在一起。 最简单的解决方案是取消Portype的@Autowired,然后在其他bean中注入ConsultasMDMWSClient:

@Controller
public class MyController {

  @Autowired
  ConsultasMDMWSClient client;

}

另一个解决方案是删除XML中的连线,然后在您的客户端中注入portype:

@Component
public class ConsultasMDMWSClientImpl implements ConsultasMDMWSClient {

  @Resource
  protected ConsultasMDMWSPortype consultasMDMWSPortype;

}

再一次,您将客户端注入其他bean。

无论如何,您都不应该在文字中硬性使用JAX-WS设置,而应在属性文件中将其替换为值,并为不同的环境准备不同的属性文件。 例如:

<bean id="consultasMDMWSPortype" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
     <property name="serviceInterface" value="${jaxws.serviceInterface}"/>    
     <property name="wsdlDocumentUrl" value="${jaxws.wsdlDocumentUrl"/>
</bean>

刚被@Qualifier替换为@Autowired。

谢谢您的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM