cxf与springmvc集成

最近,在学习cxf,想在原来的springmvc中集成,按照网上信息配置,程序正常启动,但是一直访问不到这个webservice服务,特地记录一下,集成过程中需要注意的点。

1.springmvc是通过org.springframework.web.servlet.DispatcherServlet来启动的,cxf是通过org.apache.cxf.transport.servlet.CXFServlet来启动的,CXFServlet中获取content时需要由org.springframework.web.context.ContextLoaderListener提供的content,所以需要springmvc中spring公共部分拆出xml用ContextLoaderListener来加载,mvc部分使用DispatcherServlet来加载

原文件springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

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

	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/page/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	<bean class="org.springframework.context.support.ResourceBundleMessageSource"
		id="messageSource" >
		<property name="basename" value="i18n"></property>
	</bean>
	
	<!-- 关联 properties 文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- 数据源 -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource"
		p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
		p:username="${jdbc.username}" p:password="${jdbc.password}" />
	<!-- class: 指定用来创建 sqlSession 的工厂 dataSource-ref: 使用的数据源 typeAliasesPackage: 
		自动扫描的实体类包 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
		p:dataSource-ref="dataSource" p:typeAliasesPackage="com.employ.entry" />
	<!-- class : 指定自动扫描 xxxMapper.xml 映射文件的类 basePackage: 自动扫描的配置包 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
		p:basePackage="com.employ.mapper" p:sqlSessionFactoryBeanName="sqlSessionFactory" />
	<!-- 事务管理 -->
	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
		p:dataSource-ref="dataSource" />
	<tx:annotation-driven transaction-manager="txManager" />
	
</beans>
拆分成spring-mybatis.xml 和spring-mvc.xml

spring-mybatis.xml 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

	<context:component-scan base-package="com.employ"/>
	<bean class="org.springframework.context.support.ResourceBundleMessageSource"
		id="messageSource" >
		<property name="basename" value="i18n"></property>
	</bean>
	
	<!-- 关联 properties 文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- 数据源 -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource"
		p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
		p:username="${jdbc.username}" p:password="${jdbc.password}" />
	<!-- class: 指定用来创建 sqlSession 的工厂 dataSource-ref: 使用的数据源 typeAliasesPackage: 
		自动扫描的实体类包 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
		p:dataSource-ref="dataSource" p:typeAliasesPackage="com.employ.entry" />
	<!-- class : 指定自动扫描 xxxMapper.xml 映射文件的类 basePackage: 自动扫描的配置包 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
		p:basePackage="com.employ.mapper" p:sqlSessionFactoryBeanName="sqlSessionFactory" />
	<!-- 事务管理 -->
	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
		p:dataSource-ref="dataSource" />
	<tx:annotation-driven transaction-manager="txManager" />
</beans>

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"  
	xmlns:mvc="http://www.springframework.org/schema/mvc"  
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
	<context:component-scan base-package="com.employ" /> 
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/page/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

2.拆分过程中需要注意xml文件中  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 是必须的


参考资料:cxf与springmvc No services find
参考资料:http://blog.csdn.net/weu135/article/details/13016343
参考资料:Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CXF是一个开源的Web服务框架,可以用于开发和部署SOAP和REST风格的Web服务。Spring MVC是一个基于Java的Web应用框架,用于开发和管理MVC(Model-View-Controller)模式的Web应用程序。 要集成CXF和Spring MVC,首先需要在项目中引入相关的jar包。可以通过在项目的pom.xml文件中添加依赖来实现,例如: ``` <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.4.4</version> </dependency> ``` 然后,在Spring MVC的配置文件(通常是一个XML文件,例如application-context.xml)中,需要配置CXF的相关内容。可以添加以下配置: ``` <jaxws:server id="helloService" address="/helloservice"> <jaxws:serviceBean> <bean class="com.example.HelloServiceImpl" /> </jaxws:serviceBean> </jaxws:server> ``` 在上面的配置中,`helloService`是服务的ID,`/helloservice`是服务的访问地址,`com.example.HelloServiceImpl`是实现了Web服务接口的类。 最后,在Spring MVC的控制器中,可以使用`@WebServiceRef`注解来引用CXF的Web服务。例如: ``` @Controller @RequestMapping("/hello") public class HelloController { @WebServiceRef private HelloService helloService; @RequestMapping(method = RequestMethod.GET) public String sayHello(Model model) { String message = helloService.sayHello(); model.addAttribute("message", message); return "hello"; } } ``` 在上面的示例中,`HelloService`是通过`@WebServiceRef`注解引用的CXF的Web服务接口,可以在控制器中直接调用相关的方法。 通过以上步骤,就可以实现CXF和Spring MVC的集成,从而开发和部署SOAP和REST风格的Web服务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值