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; } }

 
 

接下来就是启动发布了

 

 

 

 

 

 

 

 

猜你喜欢

转载自254633397.iteye.com/blog/1750433