7

@Autowired をカスタム cxf インターセプターに使用すると、小さな問題が発生するようです。私の使用例は、石鹸メッセージをログに記録し、AMQP を使用してこれらを別のシステムに送信することです。このプロセスは通常のサービスなどで機能します。しかし、何をしても、必要なプロパティは自動配線されず、null のままです。

Spring DI ログを確認したところ、コンテキストがスキャンされて取得されたので、何が欠けているのでしょうか?

これは CXF インターセプターでも可能ですか?

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

}
4

2 に答える 2

7

@InInterceptors(CXF アノテーション) @Component(Spring アノテーション)を混在させることはできません。これにより、インターセプターの 2 つの個別のインスタンスが作成されます。依存関係が Spring によって注入されるインスタンスと、CXF によって作成されるインスタンスです。(Bean ID ではなく、アノテーションでクラス名を提供している@InInterceptorsため、CXF は、Spring コンテキストで既にインスタンスを作成したことを知る方法がありません。)

コンポーネント スキャン@InInterceptorsに加えて、注釈を削除します。

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

アプリケーションコンテキストで次のようなものも必要です。

<jaxws:endpoint id="myWebService" address="/MyWebService">
    <jaxws:inInterceptors>
        <ref bean="myInInterceptor" />
    </jaxws:inInterceptors>
</jaxws:endpoint>
于 2012-06-04T21:23:53.357 に答える
1

これは古い質問であることは知っていますが、Jonathan Wの回答が役に立ちました。追加したいと思います。

これは、カスタムインターセプターを取得@Autowiredし、Spring Boot 1.3.1 で動作する方法です。

http://cxf.apache.org/docs/jax-ws-configuration.html

import java.util.Arrays;

import javax.jws.WebService;

import org.apache.cxf.Bus;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@EnableAutoConfiguration
@ImportResource({ "classpath:META-INF/cxf/cxf.xml" })
public class Application extends SpringBootServletInitializer {

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private MyInterceptor myInterceptor;

    @Autowired
    private HelloWorldImpl helloWorldImpl;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    // Replaces the need for web.xml
    @Bean
    public ServletRegistrationBean servletRegistrationBean(ApplicationContext context) {
        return new ServletRegistrationBean(new CXFServlet(), "/api/*");
    }

    // Replaces cxf-servlet.xml
    @Bean
    // <jaxws:endpoint id="helloWorld" implementor="demo.spring.service.HelloWorldImpl" address="/HelloWorld"/>
    public EndpointImpl helloService() {
        Bus bus = (Bus) applicationContext.getBean(Bus.DEFAULT_BUS_ID);
        EndpointImpl endpoint = new EndpointImpl(bus, helloWorldImpl);

        // Set interceptors here
        endpoint.setInInterceptors(Arrays.asList(myInterceptor));

        endpoint.publish("/hello");
        return endpoint;
    }


    // Used when deploying to a standalone servlet container, i.e. tomcat
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    // Web service endpoint
    @WebService(endpointInterface = "demo.spring.service.HelloWorld")
    //@InInterceptors not defined here
    public static class HelloWorldImpl {

    }

    public static class MyInterceptor extends LoggingInInterceptor {
      // @Autowired works here
    }

}
于 2016-03-18T07:55:46.680 に答える