2012-06-04 11 views
5

मैं कस्टम सीएक्सएफ इंटरसेप्टर में @Autowired का उपयोग करते समय एक छोटी सी समस्या में भाग लेता प्रतीत होता हूं। मेरा उपयोग मामला यह है कि मैं साबुन संदेशों को लॉग करना चाहता हूं और इन्हें एएमक्यूपी का उपयोग किसी अन्य सिस्टम में भेजना चाहता हूं। यह प्रक्रिया सामान्य सेवाओं आदि के लिए काम करती है लेकिन जो भी मैं करता हूं, आवश्यक गुण स्वचालित रूप से प्राप्त नहीं होते हैं और शून्य रहते हैं।कस्टम सीएक्सएफ इंटरसेप्टर में स्प्रिंग ऑटोवायर का उपयोग कैसे करें?

मैंने स्प्रिंग डी लॉग की जांच की और संदर्भ स्कैन और उठाया गया है, तो मुझे क्या याद आ रही है?

क्या यह सीएक्सएफ इंटरसेप्टर में भी संभव है?

@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 पाया जा रहा है और के लिए पात्र है:

यह मैं कैसे स्प्रिंग बूट 1.3.1 के साथ काम करने के लिए कस्टम इंटरसेप्टर और @Autowired मिला है वसंत कंटेनर से इंजेक्शन? क्या यह इंजेक्शन के साथ किसी भी अन्य समस्या की सूचना दी है (जैसे @Value पैरामीटर में विफल)? –

+0

क्या आप इस इंटरसेप्टर के लिए सीएक्सएफ के साथ कॉन्फ़िगरेशन साझा कर सकते हैं। इस मुद्दे का कारण यह हो सकता है कि इंटरसेप्टर को सीएक्सएफ द्वारा तत्काल किया जा सकता है और स्प्रिंग द्वारा एक अलग ऑटोवायर इंस्टेंस बनाया जा सकता है। –

+0

मैंने उपरोक्त के रूप में इंटरसेप्टर को कार्यान्वित किया है और इसे @ org.apache.cxf.interceptor के माध्यम से अपने webservice में जोड़ा है। इंटरसेप्टर्स (इंटरसेप्टर = {"org.apache.cxf.interceptor.LoggingInInterceptor", "mypackagenames.ws.interceptor.LogInInterceptor "}) मैंने बिल्कुल कोई अतिरिक्त कॉन्फ़िगरेशन नहीं किया। – Marco

उत्तर

7

आप मिश्रण नहीं कर सकते @InInterceptors (एक CXF एनोटेशन) और@Component (एक स्प्रिंग एनोटेशन)। इससे आपके इंटरसेप्टर के दो अलग-अलग उदाहरण सामने आएंगे: जिसकी निर्भरता वसंत द्वारा इंजेक्शन दी जा रही है, और एक सीएक्सएफ द्वारा बनाई गई है। (आप @InInterceptors एनोटेशन, नहीं एक सेम आईडी में वर्ग नाम प्रदान कर रहे हैं तो CXF जानते हुए भी कि आप पहले से ही वसंत संदर्भ में एक उदाहरण बनाया का कोई रास्ता नहीं है।)

component scan को @InInterceptors एनोटेशन निकालें और इसके अलावा में, :

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

आप भी अपने आवेदन के संदर्भ में कुछ इस तरह की जरूरत है: मैं जानता हूँ कि यह एक पुराने सवाल है, लेकिन जोनाथन डब्ल्यू के जवाब मुझे मदद की है और मैं जोड़ना चाहते हैं

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

घटक स्कैन सफल है, सही पैकेज नाम वसंत xml फ़ाइल में है। अभी भी गुण इंजेक्शन नहीं हैं .. कोई अन्य सुराग? – Marco

+0

हां। मैंने बिजू को आपकी प्रतिक्रिया के आधार पर अपना उत्तर संपादित किया। –

+0

यह काम कर रहा है! स्पष्ट स्पष्टीकरण के लिए धन्यवाद और इसे लागू करने में मदद करें। – Marco

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 
    } 

} 
संबंधित मुद्दे