前一节仅仅只讲了与Spring整合,没有涉及到数据库,而且是直接将Java象传递到服务端。这一节我起到一个回顾前面章节的作用。用在客户端运用JABX将JAVA对象编组成XML文件,在客户端将XML解组成JAVA并存入数据库。下面我们就着手开发这个Demo:
服务端开发
第一步,编写数据资源层相关接口
DAO编写:
- public interface HibernateDao {
- public void save(UserInfo object);
- public void delete(UserInfo object);
- public void update(UserInfo object);
- public UserInfo get(int id);
- }
DAO实现类:
- public class HibernateDaoImpl extends HibernateDaoSupport implements HibernateDao {
- public void delete(UserInfo object) {
- this.getHibernateTemplate().delete(object);
- }
- public void save(UserInfo object) {
- this.getHibernateTemplate().save(object);
- }
- public void update(UserInfo object) {
- this.getHibernateTemplate().update(object);
- }
- public UserInfo get(int id) {
- return (UserInfo) this.getHibernateTemplate().get(UserInfo.class, id);
- }
- }
实体对象:
- @Entity
- @Table(name = "userinfo")
- public class UserInfo implements java.io.Serializable {
- private static final long serialVersionUID = 2281022190032353574L;
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Integer id;
- private String name;
- private Integer age;
- private Integer address;
- public UserInfo() {
- }
- public UserInfo(String name, Integer age) {
- this.name = name;
- this.age = age;
- }
- public UserInfo(String name, Integer age, Integer address) {
- this.name = name;
- this.age = age;
- this.address = address;
- }
- public Integer getId() {
- return this.id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Integer getAge() {
- return this.age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- public Integer getAddress() {
- return this.address;
- }
- public void setAddress(Integer address) {
- this.address = address;
- }
- }
第二步:业务逻辑层接口编写
业务逻辑接口:
- @WebService
- public interface IService {
- //public void save(@WebParam(name="info")String xml);
- public void save(@WebParam(name="dto")UserInfoDTO dto,@WebParam(name="flag")boolean flag);
- public void update(@WebParam(name="info")String xml);
- public void delete(@WebParam(name="id")int id);
- public @WebResult(name="String")String get(@WebParam(name="id")int id);
- }
接口实现类:
- @WebService
- public class ServiceImpl implements IService {
- private Logger log = LoggerFactory.getLogger(ServiceImpl.class);
- private HibernateDao dao;
- // public void setDao(HibernateDao dao) {
- // this.dao = dao;
- // }
- public void delete(int id) {
- log.info("delete id is {} user"+id);
- //UserInfo userInfo = (UserInfo)object;
- //log.info("this username is:"+userInfo.getName());
- //log.info("delete {} information..."+userInfo.getName());
- //dao.delete(userInfo);
- }
- // public void save(String xml) {
- // //UserInfo userInfo = (UserInfo)object;
- // log.info("add {} user..."+xml);
- // //dao.save(userInfo);
- // System.out.println("ssss");
- // }
- public void save(UserInfoDTO dto,boolean flag) {
- System.out.println("name:"+dto.getName());
- }
- public void update(String xml){
- ApplicationContext context = new ClassPathXmlApplicationContext(
- "applicationContext-resources.xml");
- this.dao = (HibernateDao)context.getBean("dao");
- try {
- JAXBContext jaxbContext = JAXBContext.newInstance("com.itdcl.model");
- Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
- StringReader reader = new StringReader(xml);
- Customers cus = (Customers)unmarshaller.unmarshal(reader);
- Customer cust = cus.getCustomer();
- UserInfo userInfo = new UserInfo();
- System.out.println("Address:"+cust.getAddress());
- userInfo.setAddress(cust.getAddress());
- System.out.println("Age:"+cust.getAge());
- userInfo.setAge(Integer.valueOf(cust.getAge()));
- System.out.println("Name:"+cust.getName());
- //userInfo.setId(2);
- userInfo.setName(cust.getName());
- dao.save(userInfo);
- } catch (JAXBException e) {
- e.printStackTrace();
- }
- }
- public String get(int id){
- //return dao.get(id);
- return null;
- }
- }
DAO与Service接口配置:
- <?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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
- <!--*******************Hibernate数据库持久层配置文件***********************-->
- <bean id="propertyConfigurer"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:jdbc.properties</value>
- </list>
- </property>
- </bean>
- <bean id="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="${driverClassName}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="dataSource">
- <ref local="dataSource" />
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- ${hibernate.dialect}
- </prop>
- <prop key="hibernate.show_sql">${show_sql}</prop>
- <prop key="hibernate.format_sql">${format_sql}</prop>
- <!-- 缓存设置 -->
- <prop key="cache.use_query_cache">
- ${cache.use_query_cache}
- </prop>
- <prop key="cache.use_second_level_cache">
- ${cache.use_second_level_cache}
- </prop>
- <prop key="cache.provider_class">
- ${cache.provider_class}
- </prop>
- <!-- C3P0连接池配置 -->
- <prop key="c3p0.acquire_increment">
- ${c3p0.acquire_increment}
- </prop>
- <prop key="c3p0.max_statements">
- ${c3p0.max_statements}
- </prop>
- <prop key="c3p0.min_size">${c3p0.min_size}</prop>
- <prop key="c3p0.max_size">${c3p0.max_size}</prop>
- <prop key="c3p0.timeout">${c3p0.timeout}</prop>
- <prop key="c3p0.idle_test_period">
- ${c3p0.idle_test_period}
- </prop>
- </props>
- </property>
- <property name="namingStrategy">
- <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
- </property>
- <property name="packagesToScan" value="com.itdcl.pojo" />
- </bean>
- <bean id="dao" class="com.itdcl.dao.HibernateDaoImpl">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- </beans>
WebService接口发布:
- <?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:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd">
- <import resource="classpath:META-INF/cxf/cxf.xml" />
- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
- <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
- <bean id="serverUserInfo" class="com.itdcl.model.UserInfoDTO" />
- <jaxws:endpoint id="helloWorld"
- implementor="com.itdcl.service.HelloWorldImpl" address="/HelloWorld" />
- <jaxws:endpoint id="myTest"
- implementor="com.itdcl.service.MyTestImpl" address="/MyTest" />
- <jaxws:endpoint id="service"
- implementor="com.itdcl.service.ServiceImpl" address="/Service">
- <!-- jaxws:serviceFactory>
- <ref bean="jaxWsServiceFactoryBean" />
- </jaxws:serviceFactory>-->
- </jaxws:endpoint>
- <bean id="jaxWsServiceFactoryBean"
- class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
- <property name="wrapped" value="true" />
- <property name="dataBinding" ref="aegisBean" />
- </bean>
- <bean id="aegisBean"
- class="org.apache.cxf.aegis.databinding.AegisDatabinding" />
- </beans>
第三步:服务端程序编写了,打包部署
客户端开发
第一步:WebService客户端生成,可以手工编写。这里我就通过配置Ant文件来生成WebService客户端:
- <?xml version="1.0"?>
- <project name="cxf" basedir="." default="usage">
- <property name="webroot.dir" value="E:/workspace3/CXFSpring" />
- <!-- webservice接口 -->
- <property name="target.class" value="com.itdcl.service.IService" />
- <property name="wsdl.name" value="Service.wsdl" />
- <property name="src.dir" value="src" />
- <property name="client.dir" value="client" />
- <property name="server.dir" value="server" />
- <property name="compile.dir" value="${webroot.dir}/bin" />
- <target name="usage">
- <echo
- message="java2wsdl -->java文件生成wsdl文件,java2wsdl -Ddir=com/starit/cxf/java2wsdl -Dsrc=com.starit.cxf.java2wsdl.Hello" />
- <echo
- message="wsdl2javaClient -->java文件生成client,java2wsdl -Ddir=com/starit/cxf/java2wsdl -Dwsdl=hello.wsdl" />
- </target>
- <target name="java2wsdl">
- <echo message="生成wsdl文件" />
- <exec dir="${compile.dir}" executable="cmd.exe">
- <arg line="/c java2ws" />
- <arg line="-o ${wsdl.name} -wsdl ${target.class}" />
- </exec>
- <!-- copy资源文件 -->
- <move todir="${src.dir}" preservelastmodified="true">
- <fileset dir="${compile.dir}">
- <include name="**/*.wsdl" />
- </fileset>
- </move>
- </target>
- <target name="wsdl2javaClient">
- <echo message="生成java client文件" />
- <mkdir dir="${client.dir}" />
- <exec dir="." executable="cmd.exe">
- <arg line="/c wsdl2java" />
- <!-- 生成的java所存目录 -->
- <arg line="-d ${client.dir}"/>
- <!-- wsdl文件所在路径 -->
- <arg line="-client src\*.wsdl" />
- </exec>
- </target>
- </project>
第二步:WebService接口注册配置
- <?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:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd">
- <!-- jaxws:client id="helloWorldClient"
- address="http://localhost:9999/cxf/HelloWorld"
- serviceClass="com.itdcl.service.IHelloWorld" />-->
- <!-- jaxws:client id="myTestClient"
- address="http://localhost:9999/cxf/MyTest"
- serviceClass="com.itdcl.service.IMyTest" />-->
- <jaxws:client id="service"
- address="http://localhost:9999/cxf/Service"
- serviceClass="com.itdcl.service.IService" />
- </beans>
剩下的事就测试我们整合是否成功了,编写一个JUnit测试类或写一个Main函数测试一下。
- public class JaxbSampleClient {
- private final static String MODEL = "com.itdcl.model";
- public static void main(String[] args) throws FileNotFoundException, JAXBException, ParserConfigurationException, TransformerException {
- ApplicationContext context = new ClassPathXmlApplicationContext(
- "beans.xml");
- IService service = (IService) context.getBean("service");
- ObjectFactory factory = new ObjectFactory();
- Customer customer = factory.createCustomer();
- customer.setId(1);
- customer.setAge("26");
- customer.setName("Josen");
- customer.setAddress(100);
- Customers customers = factory.createCustomers();
- customers.setCustomer(customer);
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- dbf.setNamespaceAware(true);
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document doc = db.newDocument();
- // 转换
- JAXBContext jaxbContext = JAXBContext.newInstance(MODEL);
- Marshaller marshaller = jaxbContext.createMarshaller();
- //将customers数据与doc绑定
- marshaller.marshal(customers, doc);
- DOMSource domSource = new DOMSource(doc);
- StringWriter writer = new StringWriter();
- StreamResult result = new StreamResult(writer);
- TransformerFactory tf = TransformerFactory.newInstance();
- Transformer transformer = tf.newTransformer();
- transformer.transform(domSource, result);
- String xmlString = writer.toString();
- //System.out.println(xmlString);
- service.update(xmlString);
- }
- }
相关推荐
在IT行业中,构建Web服务是常见的任务之一,而CXF、Spring和Hibernate是Java领域内常用的三大框架,它们各自在不同的层面上发挥着关键作用。本项目通过整合这三者来实现一个基于SOAP协议的WebService,以提供数据...
做开发时,查了大量资料,发现一比较全面的资料,分享一下!...WebService CXF学习——高级篇(一)(二) 1.整合Spring框架 2.CXF+Spring+Hibernate 3.WS-Security WebService CXF学习——JAXB剖析
【WebService CXF Spring Hibernate】整合教程 在现代企业级应用开发中,集成各种技术框架以实现高效、可扩展和灵活的解决方案是常见的实践。本教程将深入探讨如何将`WebService`(基于SOAP协议的服务)与Java开发...
1. **配置Spring**:首先,需要在Spring配置文件中定义数据源、SessionFactory(Hibernate的配置)以及CXF的WebService配置。这通常涉及到bean的定义,包括数据访问对象(DAOs)、业务逻辑层(Service层)和Web服务...
【标题】:“cxf+hibernate3.0+spring3.0 完整整合代码实现” 【描述】:“此项目实现了将CXF、Hibernate3.0和Spring3.0这三个开源框架集成到一起,提供了在WebLogic和Tomcat两种应用服务器上的无缝兼容。” ...
【标题】"SpringMVC+Hibernate+WebService"是一个常见的企业级应用开发框架组合,用于构建高效、可扩展的Web应用程序。SpringMVC是Spring框架的一部分,负责处理HTTP请求和响应,提供模型-视图-控制器(MVC)架构。...
【标题】"CXF Spring Hibernate 搭建Web服务" 在IT行业中,Web服务是一种用于应用程序之间通信的技术,它允许不同系统之间的数据交换。本项目是关于如何利用Apache CXF、Spring框架和Hibernate ORM工具来构建一个...
这是个采用apache cxf实现webservice的示例,主要由spring整合hibernate、struts2实现webservice,里面struts2用到得较少……对于学习很有帮助,另外我觉得若是初学者,不过采用那方式,现在按照套方案做熟练后再去...
本实例"Spring+Hibernate+Struts+XFire的WebService"提供了一个全面的框架整合,用于创建功能丰富的Web服务。以下将详细解析这个实例所涉及的技术点: 1. **Spring框架**:Spring是一个开源的Java平台,它为开发...
是eclipse导出的war文件,包含了资源。开发环境jdk1.7 tomcat7。 通过了测试,对应的文档:http://blog.csdn.net/biboheart/article/details/9148651, 有文档了,这里就不多说了。
然后,定义一个服务接口和实现,使用Spring的`@WebService`注解标记接口,`@Endpoint`注解标记服务实现类。同时,配置WSDL文件生成和发布,以便客户端能够发现和调用服务。 6. **测试与调试**: 使用JUnit进行单元...
【cxf+spring 使用经验】 Apache CXF 是一个开源的 Web 服务框架,它整合了 Celtix 和 XFire 两大项目的优势,提供了全面的 JAX-WS 支持,允许开发者通过 Code First 或 WSDL First 的方式来创建和消费 Web 服务。...
使用spring集成cxf,在两个web ...server端使用spring+springmvc+mybatis+cxf,client端使用struts2+spring+hibernate+cxf 两个工程均为myeclipse project,包含所有除myeclipse自带以外所有jar 内附 使用文档,数据库脚本
1. **设置项目结构**:创建Maven或Gradle项目,引入Spring、Hibernate和CXF(用于构建Web服务)的相关依赖。 2. **配置Spring**:编写Spring配置文件,配置DataSource、SessionFactory以及事务管理器。使用`<tx:...
XDoclet是一个基于JavaDoc的代码生成工具,它可以自动根据Java源代码中的特定注解生成如EJB接口、Hibernate映射文件、Spring配置文件等。在本示例中,XDoclet可能被用来自动生成Spring配置和Web服务相关的接口及实现...
总的来说,"springmvc_cxf_hibernate.zip"是一个展示Spring MVC、CXF和Hibernate集成的实践案例,对于想要提升企业级Web应用开发技能的学习者来说,这是一个宝贵的学习资源。通过深入研究这个项目,开发者不仅可以...
CXF可与其他Java EE技术如Spring、Hibernate等无缝集成,提供更强大的功能。例如,通过Spring容器管理CXF服务,可以实现依赖注入和事务管理。 总结,CXF作为一个强大的Web服务框架,提供了从服务端到客户端的全方位...
本项目结合springMVC(4.0.3.RELEASE) + spring + hibernate(4.3.5.Final) + CXF(2.6.8)+ mysql + jquery等技术,并以Maven形式实现。具体包含以下主要功能点: 1.用CXF实现Webservice和Restful服务 2.分别使用...
SSH(Struts2、Spring、Hibernate)是一个常见的 Java Web 开发框架组合,而 CXF 是一个用于构建和开发 Web 服务的开源框架。本教程将详细介绍如何在 SSH 项目中集成 CXF 来创建和消费 WebService。 **1. 项目环境*...