java 自动装配的 Spring NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26917902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:52:28  来源:igfitidea点击:

Autowired Spring NullPointerException

javaspringweb-serviceswebservices-client

提问by Jesus Alvarez

I have a problem with my code trying to generate @Autowired.

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

The Class:

班上:

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;
    }
 }

My applicationContext.xml

我的 applicationContext.xml

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

My 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> 

I don't know what I'm doing wrong, I've mapped everything like an example of my work I'm new on Spring, my web method works without Spring.

我不知道我做错了什么,我已经将所有内容都映射为我在 Spring 上的新工作示例,我的 Web 方法在没有 Spring 的情况下也能工作。

The error just appear when I use @Autowired.

当我使用@Autowired 时,错误才出现。

java.lang.NullPointerException

Thaks everyone.

谢谢大家。

回答by whyem

As an alternative to solution proposed by @Christopher, if you want to keep the "old-style" XML configuration injection (setter injection) you need to remove @Autowiredannotation and declare a setter to ConsultasMDMWSPortype, ie:

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

ConsultasMDMWSPortype consultasMDMWSPortype;

and

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

So spring will be able to wire the ref-bean configured in xml, through the setter method.

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

回答by Jesus Alvarez

Just replaced @Autowired by @Qualifier.

刚刚用@Qualifier 替换了@Autowired。

Thanks for the help.

谢谢您的帮助。

回答by Selim Ok

You can try to add @Componentannotation on top of ConsultasMDMWSClientImplclass.

您可以尝试@ComponentConsultasMDMWSClientImpl类的顶部添加注释。

Like:

喜欢:

@Component
public class ConsultasMDMWSClientImpl implements ConsultasMDMWSClient {

This is needed to indicate that this is a spring bean, so that the spring container scan it and initialize as a spring bean while starting the spring container.

需要这个来表明这是一个spring bean,以便spring容器在启动spring容器时扫描它并初始化为spring bean。

I hope it helps.

我希望它有帮助。

回答by Christopher Yang

As already pointed out, you're mixing XML wiring with annotation wiring. The simplest solution is to take away the @Autowired of the Portype and instead inject ConsultasMDMWSClient in other beans:

正如已经指出的,您将 XML 连接与注释连接混合在一起。最简单的解决方案是去掉 Portype 的 @Autowired,而是在其他 bean 中注入 ConsultasMDMWSClient:

@Controller
public class MyController {

  @Autowired
  ConsultasMDMWSClient client;

}

Another solution would be remove the wiring in XML and just inject portype in your client:

另一种解决方案是删除 XML 中的接线,并在您的客户端中注入 portype:

@Component
public class ConsultasMDMWSClientImpl implements ConsultasMDMWSClient {

  @Resource
  protected ConsultasMDMWSPortype consultasMDMWSPortype;

}

Once again, you inject the client in other beans.

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

In any case, you shouldn't be hardwiring the JAX-WS settings in literals, you should replace them with values in properties files and prepare different properties files for different environments. For example:

在任何情况下,您都不应该在文字中硬连接 JAX-WS 设置,您应该将它们替换为属性文件中的值,并为不同的环境准备不同的属性文件。例如:

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