9

I am writing a RESTful service (using CXF on JBoss) in which I have inject another class using Spring (Autowired). But the class is not getting injected and is null.

Web Service Interface and Class (Where injection needs to happen)

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) {
      ....
    }
}

That which has to be injected

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>

My service is active (http://localhost:8080/{warname}/myws/doSomething) but the MyCore instance is not being injected into MyWebService (in the myCore field). It is always null and my service does not work as expected, instead throws NullPointerException

Tried all inputs gathered over google. No luck! Your help is highly appreciated.

Regards

Higher-Kinded Type
  • 1,964
  • 5
  • 27
  • 44

3 Answers3

8

Try to add below method to your web service:

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

The current web application context (usually the one loaded by ContextLoaderListener) will be used for autowiring, so the IMyCore bean has to be defined in the context listener configuration file and not in the web service one.

  • Right now the project has changed a bit to try the solution you have suggested. But I am definitely try it on a local copy of the code base, and lent you know. Thanks for posting! – Higher-Kinded Type Aug 09 '12 at 15:44
5

If you want to use Spring Beans in CXF Web Service class, then declare WebService as following in the XML configuration file of the CXF (e.g. spring-cxf.xml)

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

Declare separated bean for the WebService class and then put it in the endpoint with an ID. Like this you will have spring managed bean, where you can use AutoWired annotations as well.

Your beans never won't be injected automatically if you will declare your web service as following.

<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 this case you will need either:

  • Inject spring beans manually

    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

  • Or retrieve the beans one by one from the spring context

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

    I haven't tried this for JAX-RS, but the approach in my opinion should be the same.

    From CXF official documentation.

X-HuMan
  • 1,488
  • 1
  • 17
  • 37
1

Try to add below bean configuration at Beans.xml

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

In my case, it worked..

aspirant75
  • 127
  • 1
  • 2
  • 10