2012-06-04 64 views
5

當@Autowired用於自定義cxf攔截器時,我似乎遇到了一個小問題。 我的用例是我想記錄肥皂消息並使用AMQP將這些消息發送到另一個系統。這個過程適用於正常的服務等。 但無論我做什麼,所需的屬性都不會自動裝配並保持爲空。如何在自定義cxf攔截器中使用Spring Autowired?

我檢查了春季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()); 
    } 

} 
+0

通過「撿到」你的意思是你的LogInInterceptor被發現,並且有資格從Spring容器注入?它是否報告過任何其他注射問題(如@Value參數失敗)? –

+0

你可以請用CXF分享這個攔截器的配置嗎?這個問題的原因可能是攔截器可能已經被CXF實例化了,並且Spring可能創建了一個單獨的autowired實例。 –

+0

我已經實現了上面的攔截器,並通過@ org.apache.cxf.interceptor.Interceptors(interceptors = {「org.apache.cxf.interceptor.LoggingInInterceptor」,「mypackagenames.ws.interceptor.LogInInterceptor 「})我根本沒有執行任何附加配置。 – Marco

回答

7

不能混合@InInterceptors(一CXF註釋)@Component(Spring的註解)。這將創建兩個獨立的攔截器實例:Spring的依賴關係被注入的實例,以及由CXF創建的實例。 (您提供一流的名字在@InInterceptors註釋,而不是一個bean ID,所以CXF沒有辦法知道你已經創造了Spring上下文實例的方式。)

取出@InInterceptors註釋和,除了component scan

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

您還需要像這樣在你的應用環境:

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

組件掃描成功,正確的軟件包名稱位於spring xml文件中。仍然屬性不注入..任何其他線索? – Marco

+0

是的。我根據你對Biju的反饋編輯了我的答案。 –

+0

它的工作!感謝您的明確解釋並幫助實施。 – Marco

1

我知道這是一個老問題,但喬納森·W公司的回答幫我,我想補充到它。

我這是怎麼了自定義的攔截和@Autowired與Spring 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 
    } 

} 
相關問題