How to use Spring Autowired in a custom cxf interceptor?

cxf, dependency-injection, spring, web-services

Solution

You can't mix `@InInterceptors` (a CXF annotation) and `@Component` (a Spring annotation). That will create two separate instances of your interceptor: the one whose dependencies are getting injected by Spring, and one created by CXF. (You are providing class names in the `@InInterceptors` annotation, not a bean ID, so CXF has no way of knowing that you already created an instance in the Spring context.)

Remove the `@InInterceptors` annotation and, in addition to the component scan:

<context:component-scan base-package="org.example.config"/>

You also need something like this in your application context:

<jaxws:endpoint id="myWebService" address="/MyWebService">
    <jaxws:inInterceptors>
        <ref bean="myInInterceptor" />
    </jaxws:inInterceptors>
</jaxws:endpoint>

Problem

i seem to run into a small issue when using @Autowired into a custom cxf interceptor. My use case is that i want to log soap messages and send these using AMQP to another system. This process works for normal services etc. But whatever i do, the needed properties do not get autowired and stay null. I checked the Spring DI log and the context is scanned and pickedup, so what am i missing? Is this even possible in CXF interceptors? ``` @Component public class LogInInterceptor extends AbstractSoapInterceptor { private @Value("#{rabbitMQProperties['rabbitmq.binding.log.soap']}") String binding; @Autowired AmqpTemplate amqpTemplate; public LogInInterceptor() { super(Phase.RECEIVE); } @Override public void handleMessage(SoapMessage soapMessage) throws Fault { logIt(soapMessage); } private void logIt(SoapMessage message) throws Fault { // rest of the code omitted...!!! amqpTemplate.convertAndSend(binding, buffer.toString()); } } ```

Original source