Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

web services - How to use Spring Autowired in a custom cxf interceptor?

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());
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...