Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring + Apache CXF @Autowire in the service always null

I have creating soap service using Apache CXF, I have created a @WebService. In that service, I need to inject the @Service. When I @Autowire that service that instance remains null.

Endpoint initialized

@Bean
    public Endpoint endpointToken() {
        EndpointImpl endpoint = new EndpointImpl(bus, new GenerateLoginToken());



        endpoint.publish("/Token");

        return endpoint;
    }

Serivce Class

@WebService(serviceName = "GenerateToken", portName = "TokenPort",
    targetNamespace = "http://service.ws.samp",
    endpointInterface = "com.web.sigel.ws.soap.webServices.GenerateToken")
@Service("AuthService")
public class GenerateLoginToken implements  GenerateToken {

    @Autowired
    private  AuthService authService; //this remains Null whenever i make a call. 



    @Override
    @RequestWrapper(localName = "loginRequest", targetNamespace = "http://service.ws.samp", className = "com.web.sigel.ws.soap.security.LoginRequest")
    public LoginResponse generateToken(LoginRequest loginRequest) {
        LoginResponse loginResponse = new LoginResponse();
         String token  = authService.createAuthToken(loginRequest);

         loginResponse.setToken(token);

        return loginResponse;
    }
}

Is there anyway, I can inject my service.

like image 506
Muhamamd Omar Muneer Avatar asked Jan 01 '26 15:01

Muhamamd Omar Muneer


1 Answers

This is happening because you are creating a new instance of GeneratingLoginToken in your Endpoint bean:

EndpointImpl endpoint = new EndpointImpl(bus, new GenerateLoginToken());

This means Spring does not know about your new instance since it is not a Spring bean itself. Instead you should autowire GenerateLoginToken and use the Spring bean instance of this class which should have all the beans wired up to it correctly and hence AuthService should not be null:

@Autowire
GenerateLoginToken generateLoginToken;

@Bean
public Endpoint endpointToken() {
    EndpointImpl endpoint = new EndpointImpl(bus, generateLoginToken);



    endpoint.publish("/Token");

    return endpoint;
}
like image 122
Plog Avatar answered Jan 03 '26 05:01

Plog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!