Search code examples
springweb-servicesdependency-injectioncxfjax-ws

Component not injected with @Inject and Apache cxf


I have a NullPointerException on a service I try to inject with @Inject

The @WebService class :

@WebService(endpointInterface = "my.endpoint.interface.MyEndpointInterface")
public class MyEndpoint implements MyEndpointInterface {

    @Inject
    MyService myService;

    @Override
    public Response hello(Request request) {
         return myService.getHello(); // throw NullPointerException
    }
}

The MyService class is declared through @ComponentScan annotation


Solution

  • Things to check:

    1) Check if @WebService extends @Component (not sure about that) if not add @Component to the class

    @WebService(endpointInterface = "my.endpoint.interface.MyEndpointInterface")
    @Component
    public class MyEndpoint implements MyEndpointInterface {
    

    2) You're creating MyEndpoint with new(check all references). You have to get it from the Spring context. Share the cxf configuration.

    3) MyEndpoint class is not scanned by the @ComponentScan.