2012-05-04 13 views
9

Sto scrivendo un servizio RESTful (usando CXF su JBoss) nel quale ho iniettato un'altra classe usando Spring (Autowired). Ma la classe non viene iniettata ed è nulla.Il bean Spring non è stato iniettato nel servizio web CXF, perché?

interfaccia Web Service e Classe (dove l'iniezione deve accadere)

package com.company.project.web; 

@Path("/myws") 
public interface IMyWebService {  
    @POST 
    @Path("/doSomething")  
    @Consumes("application/json") 
    @Produces("application/json") 
    MyResponse doSomething(MyRequest myRequest) 
} 

@Service("myWebService") 
public class MyWebService implements IMyWebService {  
    @Autowired 
    private IMyCore myCore; 

    public MyResponse doSomething(MyRequest myRequest) { 
     .... 
    } 
} 

Ciò che deve essere iniettato

package com.company.project.biz; 

public interface IMyCore { 
    MyResponse doSomething(MyRequest myRequest); 
} 

@Component("myCore") 
public class MyCore implements IMyCore { 
    public MyResponse doSomething(MyRequest myRequest) { 
      ..... 
    } 
} 

beans.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jaxws="http://cxf.apache.org/jaxws" 
    xmlns:jaxrs="http://cxf.apache.org/jaxrs" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd  
    http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <import resource="classpath:META-INF/cxf/cxf.xml" /> 
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 
    <import resource="classpath:META-INF/cxf/cxf-extension-http.xml" /> 
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> 

    <context:annotation-config /> 
    <context:component-scan base-package="com.company.project"/>  

    <jaxrs:server id="myWebService" address="/"> 
     <jaxrs:serviceBeans> 
      <bean class="com.company.project.web.MyWebService" /> 
     </jaxrs:serviceBeans> 
     <jaxrs:extensionMappings> 
      <entry key="json" value="application/json" /> 
     </jaxrs:extensionMappings> 
    </jaxrs:server> 
</beans> 

Mio Dio rvice è attivo (http://localhost:8080/{warname}/myws/doSomething) ma l'istanza MyCore non viene iniettata in MyWebService (nel campo myCore). È sempre nullo e il mio servizio non funziona come previsto, invece lancia NullPointerException

Provato tutti gli input raccolti su google. Senza fortuna! Il tuo aiuto è molto apprezzato.

saluti

risposta

1

tenta di aggiungere sotto la configurazione di fagioli al beans.xml

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/> 

Nel mio caso, ha funzionato ..

+0

Ho provato questo e non funziona per me. Sembra che il servizio web non sia stato creato da Spring e quindi non viene eseguito automaticamente. –

8

tenta di aggiungere sotto metodo per il servizio web:

@PostConstruct 
public void init() { 
    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); 
} 

Il contesto dell'applicazione Web corrente (in genere quello caricato da ContextLoaderListen er) verrà utilizzato per l'autowiring, quindi il bean IMyCore deve essere definito nel file di configurazione del listener di contesto e non in quello del servizio web.

+0

In questo momento il progetto è cambiato un po 'per provare la soluzione che hai suggerito. Ma sicuramente lo proverò su una copia locale della base di codice e ti presterò sapere. Grazie per la pubblicazione! –

+0

Questo ha funzionato per me ... Grazie !!!! : D –

5

Se si desidera utilizzare Fagioli Primavera in classe CXF Web Service, quindi dichiarare WebService come segue nel file di configurazione XML del CXF (ad esempio, la primavera-cxf.xml)

<bean id="hello" class="demo.spring.service.HelloWorldImpl" /> 
<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /> 

Declare fagioli separato per la Classe WebService e quindi inserirla nell'endpoint con un ID. In questo modo avrai un bean gestito in primavera, dove puoi anche usare le annotazioni AutoWired.

I tuoi fagioli non verranno mai iniettati automaticamente se dichiarerai il tuo servizio web come segue.

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> 
<import resource="classpath:META-INF/cxf/cxf.xml"/> 
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> 
<jaxws:endpoint id="helloWorld" implementor="demo.spring.service.HelloWorldImpl" address="/HelloWorld"/> 

In questo caso sarà necessario uno:

  • Iniettare fagioli molla manualmente

    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

  • o recuperare i fagioli uno per uno dalla primavera contesto

    ApplicationContext context = ...; // your Spring ApplicationContext HelloBean helloBean = (HelloBean) context.getBean("bean");

    Non ho provato questo per JAX-RS, ma l'approccio a mio parere dovrebbe essere lo stesso.

    From CXF official documentation.

Problemi correlati