`
临听岗岗
  • 浏览: 9727 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

cxf + spring 开发基于web服务的分布式异构数据同步更新应用技术研究

 
阅读更多

本课题主要解决不同平台和不同应用系统之间的数据转换、数据同步和多个数据源的共享集成问题。为解决该问题,采用基于Web服务的方式来进行数据的交互,主要要考虑如何保持两个系统中的数据能实时同步更新(增加、删除和修改)。

用cxf-2.6.1+spring 3.1.0+ hibernate3.6 版本开发web services

 

首先开发服务端

接口 IHelloService

 

package com.dx.service; import javax.jws.WebMethod; import javax.jws.WebService; import com.dx.model.User; @WebService public interface IHelloService { @WebMethod public String sayHello(String username); @WebMethod public User getUser(int id); @WebMethod public void add(User user); }

 

 IHelloService 的实现 IHelloServiceImpl

package com.dx.service.impl; import javax.jws.WebMethod; import javax.jws.WebService; import com.dx.dao.BaseDao; import com.dx.model.User; import com.dx.service.IHelloService; @WebService public class IHelloServiceImpl implements IHelloService{ private BaseDao baseDao; @Override @WebMethod public String sayHello(String username) { return "Hello "+username; } @Override @WebMethod public User getUser(int id) { User user = new User(); user.setName("杨钰莹"); user.setId(id); return user; } @Override @WebMethod public void add(User user) { baseDao.save(user); } public BaseDao getBaseDao() { return baseDao; } public void setBaseDao(BaseDao baseDao) { this.baseDao = baseDao; } }

 

 

spring的配置文件 applicationContext-config.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> </beans>

 


 

jdbc.properties 数据库属性文件

connection.driver_class=com.mysql.jdbc.Driver jdbc.connection.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8 jdbc.connection.username=root jdbc.connection.password=**** jdbc.pool.c3p0.acquire_increment=2 jdbc.pool.c3p0.max_size=20 jdbc.pool.c3p0.min_size=2 jdbc.pool.c3p0.idle_connection_test_period=18000

 


 

spring 的另一配置文件 applicationContext-dataAccess.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${connection.driver_class}"/> <property name="jdbcUrl" value="${jdbc.connection.url}"/> <property name="idleConnectionTestPeriod" value="${jdbc.pool.c3p0.idle_connection_test_period}" /> <property name="properties"> <props> <prop key="user">${jdbc.connection.username}</prop> <prop key="password">${jdbc.connection.password}</prop> <prop key="c3p0.acquire_increment">${jdbc.pool.c3p0.acquire_increment}</prop> <prop key="c3p0.max_size">${jdbc.pool.c3p0.max_size}</prop> <prop key="c3p0.min_size">${jdbc.pool.c3p0.min_size}</prop> </props> </property> </bean> <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="myDataSource"/> <property name="mappingDirectoryLocations"> <list> <value>classpath*:persist/user</value> </list> </property> <property name="hibernateProperties"> <props> <!--常用数据库方言 MySQL5Dialect,SQLServerDialect,OracleDialect,SybaseDialect,DB2Dialect,HSQLDialect --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <!-- <prop key="hibernate.query.substitutions">true 1, false 0</prop> <prop key="hibernate.default_batch_fetch_size">4</prop> --> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="mySessionFactory"/> </bean> <aop:config> <aop:pointcut id="productServiceMethods" expression="execution(* com.dx.dao.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save" propagation="REQUIRED"/> <tx:method name="update" propagation="REQUIRED"/> <tx:method name="*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <bean id="baseDao" class="com.dx.dao.impl.BaseDaoImpl"> <property name="sessionFactory" ref="mySessionFactory"></property> </bean> </beans>

 


 

spring 的用户配置文件 applicationContext-user.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:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:endpoint id="hellos" address="/Hellows" implementor="#helloService"> </jaxws:endpoint> <bean id="helloService" class="com.dx.service.impl.IHelloServiceImpl"> <property name="baseDao" ref="baseDao"></property> </bean> </beans>

 


 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"> <display-name>cxf</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:context/applicationContext-*.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>60</session-timeout> </session-config> </web-app>

 


 

接口 BaseDao

package com.dx.dao; import java.io.Serializable; import java.util.List; import org.hibernate.SQLQuery; public interface BaseDao { public void save(Object object); public void update(Object object); public void delete(Object object); public <T> T get(Class<T> type,Serializable id); public List<?> query(String hql); public List<?> query(String hql,int pageNum,int pageSize); public List<?> queryBySql(String sql); public List<?> queryBySql(SQLQuery sqlQuery); public List<?> queryBySql(String sql,int pageNum,int pageSize); public List<?> queryBySql(SQLQuery sqlQuery,int pageNum,int pageSize); }

 


 

baseDao 的实现 baseDaoImpl

 

package com.dx.dao.impl; import java.io.Serializable; import java.util.List; import org.hibernate.Hibernate; import org.hibernate.Query; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.dx.dao.BaseDao; public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao{ private SessionFactory sessionFactory; public void setSuperSessionFactory(SessionFactory sessionFactory) { super.setSessionFactory(sessionFactory); } @Override public void save(Object object) { this.getHibernateTemplate().save(object); } @Override public void update(Object object) { this.getHibernateTemplate().update(object); } @Override public void delete(Object object) { this.getHibernateTemplate().delete(object); } @Override public <T> T get(Class<T> type, Serializable id) { return this.getHibernateTemplate().get(type, id); } @Override public List<?> query(String hql) { return this.getHibernateTemplate().find(hql); } @Override public List<?> query(String hql, int pageNum, int pageSize) { List<?> list=null; Session session = this.getSession(); Transaction transaction = session.beginTransaction(); transaction.begin(); Query query = session.createQuery(hql); query.setFirstResult(pageNum*pageSize); query.setMaxResults(pageSize); list = query.list(); transaction.commit(); session.close(); return list; } @Override public List<?> queryBySql(String sql) { List<?> list=null; Session session = this.getSession(); Transaction transaction = session.beginTransaction(); transaction.begin(); SQLQuery sqlQuery = session.createSQLQuery(sql); list = sqlQuery.list(); transaction.commit(); session.close(); return list; } @Override public List<?> queryBySql(String sql, int pageNum, int pageSize) { List<?> list=null; Session session = this.getSession(); Transaction transaction = session.beginTransaction(); transaction.begin(); SQLQuery query = session.createSQLQuery(sql); query.setFirstResult(pageNum*pageSize); query.setMaxResults(pageSize); list = query.list(); transaction.commit(); session.close(); return list; } @Override public List<?> queryBySql(SQLQuery sqlQuery) { List<?> list=null; Session session = this.getSession(); Transaction transaction = session.beginTransaction(); transaction.begin(); return null; } @Override public List<?> queryBySql(SQLQuery sqlQuery, int pageNum, int pageSize) { // TODO Auto-generated method stub return null; } }

 


 

接下来就是启动发布了

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    Java.Web服务开发

    Java Web服务开发是构建分布式应用程序的关键技术,它允许不同系统之间的数据交换和功能共享。Web服务通常基于开放标准,如XML(可扩展标记语言)、SOAP(简单对象访问协议)、WSDL(Web服务描述语言)以及UDDI...

    WebService详细解析(axis,xfire,cxf,授权认证加密解密)

    WebService是一种基于XML的Web应用程序接口标准,它允许不同的系统和服务之间进行互操作,通过HTTP协议传输数据,使得分布式系统间的通信变得更加简单。WebService的核心技术包括SOAP(Simple Object Access ...

    SOA实践者说分布式环境下的系统集成实例源码ch6

    源码部分可能涵盖了上述概念的实际实现,例如使用Spring Boot构建服务,利用Apache CXF或Spring Cloud Netflix Eureka进行服务注册与发现,使用Apache Camel或Netflix Zuul实现服务路由,以及通过Apache Kafka或...

    soa.rar_SOA_数据交换平台

    Web服务是实现SOA的主要技术手段,通常基于WSDL(Web Services Description Language)描述服务接口,使用SOAP(Simple Object Access Protocol)进行数据交换,通过UDDI(Universal Description, Discovery, and ...

    Servicemix(ESB)发布WebService.doc

    - **CXF与Servicemix**:Apache CXF是一个流行的Web服务框架,它与Servicemix紧密集成,提供方便的服务开发和发布功能。 - **配置CXF组件**:在Servicemix中,需要配置CXF服务引擎,包括指定服务接口和实现类,...

    微服务治理框架的技术选型.docx

    3. ConsulConsul 是 HashiCorp 公司开发的一款分布式服务发现和配置工具,支持多数据中心,提供了服务发现、健康检查、KV 存储、多数据中心的解决方案。Consul 以其简单易用和健壮性受到欢迎,可以与多种编程语言...

    解析soa思想与相关技术.pdf

    4. **异构系统集成**:SOA能够轻松连接不同平台、技术和数据源,实现跨系统的互操作性。 在SOA实现过程中,涉及的关键技术主要包括: 1. **Web服务**:Web服务是SOA中最常见的服务实现方式,如SOAP(Simple Object...

    webService

    ### WebService技术与Java中的CXF框架应用 #### 一、WebService概述 WebService是一种跨语言、跨平台的应用间通信协议,允许在分布式环境下构建松耦合、标准的应用组件。通过XML、SOAP、WSDL和UDDI等标准,...

    camel-manual-1.5.0.pdf

    - **Apache CXF**:一套智能Web服务解决方案(JAX-WS)。 - **Apache MINA**:一款网络框架。 - **Apache ServiceMix**:最流行且功能强大的分布式开源企业服务总线(ESB)和JBI容器之一。 #### 二、架构与特性 ...

    Mule in action下载(英文版)

    《Mule in Action》一书深入探讨了Mule——一个轻量级消息框架与高度分布式的对象代理系统,为读者提供了全面的理论与实践指导。本书由David Dossot和John D'Emic共同撰写,旨在帮助开发者掌握Mule的核心功能与配置...

    Mule stdio 安装过程

    Mule ESB,全称Mule Enterprise Service Bus,是一个基于Java的企业服务总线,专注于简化分布式系统的集成。作为轻量级的消息框架和整合平台,Mule ESB遵循Enterprise Integration Patterns(EIP)的设计原则,旨在...

Global site tag (gtag.js) - Google Analytics