6

我目前正在开发一个基于 Spring MVC 的项目,它只是一个使用 Spring MVC 模板的标准项目。所以我有 web.xml 和 servlet-context.xml。

我正在努力将 Apache cxf Web 服务添加到这个项目中,并遇到与现有 Spring MVC 共享服务的一些问题。

我最初的方法是尝试让 Web 服务正常工作,所以我的 web.xml 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets 
        and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml
        /WEB-INF/spring/jaxwsServlet/jaxwsServlet-context.xml
        </param-value>
    </context-param>




    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Process web service requests -->
    <servlet>
        <servlet-name>jaxws</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jaxws</servlet-name>
        <url-pattern>/industryAspectWS</url-pattern>
    </servlet-mapping>


    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

和我的 jaxwsServlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:beans="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:ws="http://jax-ws.dev.java.net/spring/core"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://jax-ws.dev.java.net/spring/core
        classpath:spring-jax-ws-core.xsd
        http://jax-ws.dev.java.net/spring/servlet
        classpath:spring-jax-ws-servlet.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>   
    <wss:binding url="/industryAspectWS">
        <wss:service>
            <ws:service bean="#industryAspectWS"/>
        </wss:service>
    </wss:binding>

    <!-- Web service methods -->
    <bean id="industryAspectWS" class="com.example.ws.IndustryAspectWS"></bean>

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

    <beans:import resource="../HibernateTransaction.xml" />

    <beans:import resource="../JaxbMarshaller.xml" />

</beans>

我从 servlet-context.xml复制了context:component-scan beans:import部分

此配置工作正常,因为我能够启动服务器、调用 Web 服务并访问 servlet 和 jsp。但是,我注意到,由于我在两个上下文 xml 文件中都引用了 hibernateTransaction.xml,所以有两个会话工厂。

我想在 Apache cxf 和 Spring MVC 控制器之间共享所有服务(例如休眠),所以我尝试将设置放在 root-context.xml 中,但没有成功。我也尝试在网上搜索这个,但没有找到任何关于共享服务的完整示例。我们是否可以设置在两者之间共享服务?

=================================================

发布此消息后,我尝试了一些设置,并认为如果我将

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

<tx:annotation-driven transaction-manager="txManagerExample" />

在 servlet-context.xml 和 jaxwsServlet-context.xml 中,它都能正常工作。所有其他设置都可以保留在共享的 root-context.xml 中

4

2 回答 2

8

WSSpringServlet不是CXF。是地铁。我建议使用 CXF。在这种情况下,您将拥有一个CXFServlet,但随后您将在主 Spring 上下文中设置 CXF(由ContextLoaderListener.

它将按如下方式工作:您的主要上下文将包含所有共享 bean,包括 CXF。您的 Servlet 上下文将只有您的控制器。由于 servlet 上下文是主上下文的子上下文,因此您的控制器也可以访问主上下文中的所有内容。

请参阅Embedding CXF inside of Spring 页面CXF Servlet 传输页面,以及我对这个关于在 servlet 上下文和主上下文之间共享 bean的问题的回答。

于 2012-08-31T20:45:15.860 回答
0

感谢https://stackoverflow.com/a/30758664/2615824

两个调度程序(Spring MVC REST 控制器和 CXF JAX-WS)和一个 Spring 上下文,Java Config(没有 xml 的东西......)示例:

WebApplicationInitializer:

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(MyServiceConfig.class);
    servletContext.addListener(new ContextLoaderListener(rootContext));

    AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
    webContext.setParent(rootContext);

    ServletRegistration.Dynamic restDispatcher = servletContext.addServlet("REST dispatcher", new DispatcherServlet(webContext));
    restDispatcher.setLoadOnStartup(1);
    restDispatcher.addMapping("/api/*");

    ServletRegistration.Dynamic cxfDispatcher = servletContext.addServlet("CXF dispatcher", CXFServlet.class);
    cxfDispatcher.setLoadOnStartup(1);
    cxfDispatcher.addMapping("/services/*");
}

配置:

@Configuration
@ComponentScan("my.root.package")
@ImportResource(value = {"classpath:META-INF/cxf/cxf.xml"})
@PropertySource("classpath:app-env.properties")
@PropertySource("classpath:app.properties")
@EnableWebMvc
public class MyServiceConfig {

    @Autowired
    private Bus cxfBus;

    @Autowired
    private CxfEndpointImpl cxfEndpoint;

    @Bean
    public Endpoint cxfService() {
        EndpointImpl endpoint = new EndpointImpl(cxfBus, cxfEndpoint);
        endpoint.setAddress("/CxfEndpointImpl");
        endpoint.setWsdlLocation("classpath:CxfService/CxfService-v1.0.wsdl");
        endpoint.publish();
        return endpoint;
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

示例 CXF 端点(首先是 WSDL):

@Component
@WebService(
        endpointInterface = ".....v1_0.CxfServicePort",
        targetNamespace = "http://..../service/CxfService/v1_0",
        serviceName = "CxfService",
        portName = "CxfServicePort",
        wsdlLocation = "classpath:CxfService/CxfService-v1.0.wsdl")
@MTOM(enabled = true)
@SchemaValidation(type = SchemaValidationType.BOTH) 
@InInterceptors(classes = NoBinaryContentLoggingInInterceptor.class)
@OutInterceptors(classes = NoBinaryContentLoggingOutInterceptor.class)
@EndpointProperty(key = "ws-security.callback-handler", ref = "cxfEndpointSecurityHandler")
public class CxfEndpointImpl implements CxfServicePort {

    //...

}

示例 REST 控制器:

@RestController
@RequestMapping(value = "/system", produces = MediaType.APPLICATION_JSON_VALUE)
public class RestController {

    //...
}

CXF Endpoint部署地址:

ip:port/tomcat-context/services/CxfEndpointImpl?wsdl

Spring REST Controller 部署地址:

ip:port/tomcat-context/api/system
于 2017-04-19T23:41:58.540 回答