Apache CXF + Spring Java config (no XML)

前端 未结 5 2176
孤城傲影
孤城傲影 2020-12-15 19:05

Trying to deploy a JAX-WS endpoint using Tomcat 7 Maven plugin and CXF 2.7.8. As a matter of preference, I don\'t want to have any XML config for Spring or CXF. I see severa

5条回答
  •  生来不讨喜
    2020-12-15 19:53

    This thread definitely put me on the right track to getting CXF to run in pure Spring Java configuration, but it didn't provide everything that is required.

    For my self, pure Java configuration means without a web.xml file, which I think this answer assumes is present. Spring Boot for example doesn't use a web.xml file.

    So to register a CXF endpoint without the use of any XML files at all you will need a configuration file that also loads the CXFServlet.

    import org.apache.cxf.Bus;
    import org.apache.cxf.jaxws.EndpointImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.ImportResource;
    
    import javax.xml.ws.Endpoint;
    
    @Configuration
    @ImportResource({"classpath:META-INF/cxf/cxf.xml"})
    public class JavaConfiguration {
    
        @Autowired
        private Bus bus;
    
        @Bean
        public Endpoint myServiceEndpoint() {
            EndpointImpl endpoint = new EndpointImpl(bus, new MyService());
            endpoint.publish("/myservice");
            return endpoint;
        }
    
        @Bean
        public ServletRegistrationBean cxfServlet() {
            ServletRegistrationBean servlet = new ServletRegistrationBean(new CXFServlet(), "/services/*");
            servlet.setLoadOnStartup(1);
            return servlet;
        }
    }
    

    The above is all the configuration required to successfully load a CXF endpoint within Spring.

    I have also created a small project that demonstrates this.

提交回复
热议问题