Autowired Spring NullPointerException

16k views Asked by At

I have a problem with my code trying to generate @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

      <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

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

The error just appear when I use @Autowired.

java.lang.NullPointerException

Thaks everyone.

4

There are 4 answers

2
Selim Ok On

You can try to add @Component annotation on top of ConsultasMDMWSClientImpl class.

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.

I hope it helps.

0
Christopher Yang On

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:

@Controller
public class MyController {

  @Autowired
  ConsultasMDMWSClient client;

}

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

@Component
public class ConsultasMDMWSClientImpl implements ConsultasMDMWSClient {

  @Resource
  protected ConsultasMDMWSPortype consultasMDMWSPortype;

}

Once again, you inject the client in other beans.

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:

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

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 @Autowired annotation and declare a setter to ConsultasMDMWSPortype, ie:

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.

0
Jesus Alvarez On

Just replaced @Autowired by @Qualifier.

Thanks for the help.