2014-11-13 3 views
0

@Autowired 생성을 시도하는 코드에 문제가 있습니다.Autowired Spring NullPointerException

더 클래스 : 내가 잘못 뭘하는지 모르는

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

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에, 내 작업의 예와 같은 모든 것을 매핑했습니다. 저는 Spring에서 새로운데, Spr없이 웹 메서드가 작동합니다. ing.

@Autowired를 사용하면 오류가 나타납니다.

java.lang.NullPointerException 

탁아 모두.

+0

당신이 코드를 추가 할 수 및 액세스 ConsultasMDMWSClientImpl? 내 생각 엔 콩을 사용하지 않고 직접 인스턴스를 만드는 것입니다 ... –

+0

당신은 참조를 Autowiring하고 XML로 설정해야합니다 : @Autowired ConsultasMDMWSPortype consultasMDMWSPortype; – BillFromHawaii

답변

0

ConsultasMDMWSClientImpl 클래스 위에 @Component 주석을 추가 할 수 있습니다.

처럼 :

@Component 
public class ConsultasMDMWSClientImpl implements ConsultasMDMWSClient { 

이것은 스프링 컨테이너를 스캔하고 스프링 컨테이너를 시작하는 동안 스프링 빈으로 초기화 할 수 있도록이, 스프링 빈을 나타 내기 위해 필요하다.

도움이되기를 바랍니다.

+0

'ConsultasMDMWSClientImpl'은 XML에서 이미 정의 되었기 때문에 xml/annotation 구성의 이상한 혼합이고 그는'consultasMDMWSClient'를 두 번 배선하기 때문에 필요하지 않습니다. – whyem

+0

Ohh s * 간단히 간과하겠습니다. 충고에 감사하다. // 실제로 consultAsMDMWSPortype 속성이 XML에서 alredy로 설정되었으므로 @Autowired가 중복됩니다. 따라서이 경우 Null 값은 여기서 예상되어서는 안됩니다. 기묘한. –

0

이미 지적했듯이 XML 배선과 주석 배선이 섞여 있습니다. 가장 간단한 해결책은 다른 콩에 ConsultasMDMWSClient을 Portype의 @Autowired를 빼앗아 대신 주입하는 것입니다

@Controller 
public class MyController { 

    @Autowired 
    ConsultasMDMWSClient client; 

} 

또 다른 해결책은 XML에 배선을 제거하고 당신의 클라이언트에 portype를 주입 것 :

@Component 
public class ConsultasMDMWSClientImpl implements ConsultasMDMWSClient { 

    @Resource 
    protected ConsultasMDMWSPortype consultasMDMWSPortype; 

} 

다시 한 번, 다른 빈에 클라이언트를 주입합니다.

어떤 경우 든 리터럴에서 JAX-WS 설정을 하드 와이어해서는 안되며, 특성 파일의 값으로 바꿔야하고 다른 환경의 다른 특성 파일을 준비해야합니다. 당신은 @Autowired 주석을 제거하고 ConsultasMDMWSPortype에 세터를 선언해야 "오래된 스타일"XML 구성 분사 (세터 주입)을 유지하려는 경우, @Christopher에 의해 제안 된 솔루션에 대한 대안으로

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

: 예를 들어, 즉 :

ConsultasMDMWSPortype consultasMDMWSPortype; 

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

그래서 봄이 setter 메소드를 통해 XML로 구성 심판 콩을 연결할 수있을 것입니다 .

0

@Authowired @ @Qualifier로 바 꾸었습니다.

도움 주셔서 감사합니다.

0

"public"을 개체 앞에 붙이는이 문제를 해결했습니다.

예 : 사용자가 만든 곳

@Autowired 
public ConsultasMDMWSPortype consultasMDMWSPortype; 
관련 문제