WebService CXF学习(高级篇2):CXF+Spring+Hibernate

  前一节仅仅只讲了与Spring整合,没有涉及到数据库,而且是直接将Java象传递到服务端。这一节我起到一个回顾前面章节的作用。用在客户端运用JABX将JAVA对象编组成XML文件,在客户端将XML解组成JAVA并存入数据库。下面我们就着手开发这个Demo: 
    服务端开发 
    第一步,编写数据资源层相关接口 
    DAO编写: 
   

Java代码    收藏代码
  1. public interface HibernateDao {  
  2.       
  3.     public void save(UserInfo object);  
  4.     public void delete(UserInfo object);  
  5.     public void update(UserInfo object);  
  6.     public UserInfo get(int id);  
  7. }  
  8.       



    DAO实现类:

Java代码    收藏代码
  1. public class HibernateDaoImpl extends HibernateDaoSupport implements HibernateDao {  
  2.   
  3.     public void delete(UserInfo object) {  
  4.         this.getHibernateTemplate().delete(object);  
  5.     }  
  6.   
  7.     public void save(UserInfo object) {  
  8.         this.getHibernateTemplate().save(object);  
  9.     }  
  10.   
  11.     public void update(UserInfo object) {  
  12.         this.getHibernateTemplate().update(object);  
  13.     }  
  14.   
  15.     public UserInfo get(int id) {  
  16.         return (UserInfo) this.getHibernateTemplate().get(UserInfo.class, id);  
  17.     }  
  18.   
  19. }  


      实体对象:

Java代码    收藏代码
  1. @Entity  
  2. @Table(name = "userinfo")  
  3. public class UserInfo implements java.io.Serializable {  
  4.   
  5.     private static final long serialVersionUID = 2281022190032353574L;  
  6.       
  7.     @Id  
  8.     @GeneratedValue(strategy = GenerationType.IDENTITY)  
  9.     private Integer id;  
  10.     private String name;  
  11.     private Integer age;  
  12.     private Integer address;  
  13.   
  14.     public UserInfo() {  
  15.     }  
  16.   
  17.     public UserInfo(String name, Integer age) {  
  18.         this.name = name;  
  19.         this.age = age;  
  20.     }  
  21.   
  22.     public UserInfo(String name, Integer age, Integer address) {  
  23.         this.name = name;  
  24.         this.age = age;  
  25.         this.address = address;  
  26.     }  
  27.   
  28.     public Integer getId() {  
  29.         return this.id;  
  30.     }  
  31.   
  32.     public void setId(Integer id) {  
  33.         this.id = id;  
  34.     }  
  35.   
  36.     public String getName() {  
  37.         return this.name;  
  38.     }  
  39.   
  40.     public void setName(String name) {  
  41.         this.name = name;  
  42.     }  
  43.   
  44.     public Integer getAge() {  
  45.         return this.age;  
  46.     }  
  47.   
  48.     public void setAge(Integer age) {  
  49.         this.age = age;  
  50.     }  
  51.   
  52.     public Integer getAddress() {  
  53.         return this.address;  
  54.     }  
  55.   
  56.     public void setAddress(Integer address) {  
  57.         this.address = address;  
  58.     }  
  59.   
  60. }  



      第二步:业务逻辑层接口编写 
      业务逻辑接口:

Java代码    收藏代码
  1. @WebService  
  2. public interface IService {  
  3.   
  4.     //public void save(@WebParam(name="info")String xml);  
  5.     public void save(@WebParam(name="dto")UserInfoDTO dto,@WebParam(name="flag")boolean flag);  
  6.     public void update(@WebParam(name="info")String xml);  
  7.     public void delete(@WebParam(name="id")int id);  
  8.     public @WebResult(name="String")String get(@WebParam(name="id")int id);  
  9. }  


     接口实现类:

Java代码    收藏代码
  1. @WebService  
  2. public class ServiceImpl implements IService {  
  3.     private Logger log = LoggerFactory.getLogger(ServiceImpl.class);  
  4.     private HibernateDao dao;  
  5.       
  6. //  public void setDao(HibernateDao dao) {  
  7. //      this.dao = dao;  
  8. //  }  
  9.   
  10.     public void delete(int id) {  
  11.         log.info("delete id is {} user"+id);  
  12.         //UserInfo userInfo = (UserInfo)object;  
  13.         //log.info("this username is:"+userInfo.getName());  
  14.         //log.info("delete {} information..."+userInfo.getName());  
  15.         //dao.delete(userInfo);  
  16.     }  
  17.   
  18. //  public void save(String xml) {  
  19. //      //UserInfo userInfo = (UserInfo)object;  
  20. //      log.info("add {} user..."+xml);  
  21. //      //dao.save(userInfo);  
  22. //      System.out.println("ssss");  
  23. //  }  
  24.       
  25.     public void save(UserInfoDTO dto,boolean flag) {  
  26.         System.out.println("name:"+dto.getName());  
  27.     }  
  28.   
  29.     public void update(String xml){  
  30.         ApplicationContext context = new ClassPathXmlApplicationContext(  
  31.         "applicationContext-resources.xml");  
  32.         this.dao = (HibernateDao)context.getBean("dao");  
  33.         try {  
  34.             JAXBContext jaxbContext = JAXBContext.newInstance("com.itdcl.model");  
  35.             Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();  
  36.             StringReader reader = new StringReader(xml);  
  37.             Customers cus = (Customers)unmarshaller.unmarshal(reader);  
  38.             Customer cust = cus.getCustomer();  
  39.               
  40.             UserInfo userInfo = new UserInfo();  
  41.             System.out.println("Address:"+cust.getAddress());  
  42.             userInfo.setAddress(cust.getAddress());  
  43.             System.out.println("Age:"+cust.getAge());  
  44.             userInfo.setAge(Integer.valueOf(cust.getAge()));  
  45.             System.out.println("Name:"+cust.getName());  
  46.             //userInfo.setId(2);  
  47.             userInfo.setName(cust.getName());  
  48.               
  49.             dao.save(userInfo);  
  50.         } catch (JAXBException e) {  
  51.             e.printStackTrace();  
  52.         }  
  53.           
  54.     }  
  55.       
  56.     public String get(int id){  
  57.         //return dao.get(id);  
  58.         return null;  
  59.     }  
  60. }  



     DAO与Service接口配置:

Java代码    收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  7.     xsi:schemaLocation="  
  8.     http://cxf.apache.org/jaxws   
  9.     http://cxf.apache.org/schemas/jaxws.xsd  
  10.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  11.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd  
  12.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
  13.   
  14.     <!--*******************Hibernate数据库持久层配置文件***********************-->  
  15.     <bean id="propertyConfigurer"  
  16.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  17.         <property name="locations">  
  18.             <list>  
  19.                 <value>classpath:jdbc.properties</value>  
  20.             </list>  
  21.         </property>  
  22.     </bean>  
  23.   
  24.     <bean id="dataSource"  
  25.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  26.         <property name="driverClassName" value="${driverClassName}" />  
  27.         <property name="url" value="${jdbc.url}" />  
  28.         <property name="username" value="${jdbc.username}" />  
  29.         <property name="password" value="${jdbc.password}" />  
  30.     </bean>  
  31.   
  32.     <bean id="sessionFactory"  
  33.         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  34.         <property name="dataSource">  
  35.             <ref local="dataSource" />  
  36.         </property>  
  37.         <property name="hibernateProperties">  
  38.             <props>  
  39.                 <prop key="hibernate.dialect">  
  40.                     ${hibernate.dialect}  
  41.                 </prop>  
  42.                 <prop key="hibernate.show_sql">${show_sql}</prop>  
  43.                 <prop key="hibernate.format_sql">${format_sql}</prop>  
  44.                 <!-- 缓存设置 -->  
  45.                 <prop key="cache.use_query_cache">  
  46.                     ${cache.use_query_cache}  
  47.                 </prop>  
  48.                 <prop key="cache.use_second_level_cache">  
  49.                     ${cache.use_second_level_cache}  
  50.                 </prop>  
  51.                 <prop key="cache.provider_class">  
  52.                     ${cache.provider_class}  
  53.                 </prop>  
  54.   
  55.                 <!-- C3P0连接池配置 -->  
  56.                 <prop key="c3p0.acquire_increment">  
  57.                     ${c3p0.acquire_increment}  
  58.                 </prop>  
  59.                 <prop key="c3p0.max_statements">  
  60.                     ${c3p0.max_statements}  
  61.                 </prop>  
  62.                 <prop key="c3p0.min_size">${c3p0.min_size}</prop>  
  63.                 <prop key="c3p0.max_size">${c3p0.max_size}</prop>  
  64.                 <prop key="c3p0.timeout">${c3p0.timeout}</prop>  
  65.                 <prop key="c3p0.idle_test_period">  
  66.                     ${c3p0.idle_test_period}  
  67.                 </prop>  
  68.             </props>  
  69.         </property>  
  70.         <property name="namingStrategy">  
  71.             <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />  
  72.         </property>  
  73.         <property name="packagesToScan" value="com.itdcl.pojo" />  
  74.     </bean>  
  75.   
  76.     <bean id="dao" class="com.itdcl.dao.HibernateDaoImpl">  
  77.         <property name="sessionFactory" ref="sessionFactory" />  
  78.     </bean>  
  79. </beans>  



     WebService接口发布:

Java代码    收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.     xsi:schemaLocation="  
  6.         http://www.springframework.org/schema/beans   
  7.         http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  8.         http://cxf.apache.org/jaxws   
  9.         http://cxf.apache.org/schemas/jaxws.xsd">  
  10.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  11.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  12.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  13.   
  14.     <bean id="serverUserInfo" class="com.itdcl.model.UserInfoDTO" />  
  15.     <jaxws:endpoint id="helloWorld"  
  16.         implementor="com.itdcl.service.HelloWorldImpl" address="/HelloWorld" />  
  17.   
  18.     <jaxws:endpoint id="myTest"  
  19.         implementor="com.itdcl.service.MyTestImpl" address="/MyTest" />  
  20.   
  21.     <jaxws:endpoint id="service"  
  22.         implementor="com.itdcl.service.ServiceImpl" address="/Service">  
  23.         <!--  jaxws:serviceFactory>  
  24.             <ref bean="jaxWsServiceFactoryBean" />  
  25.         </jaxws:serviceFactory>-->  
  26.     </jaxws:endpoint>  
  27.   
  28.     <bean id="jaxWsServiceFactoryBean"  
  29.         class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">  
  30.         <property name="wrapped" value="true" />  
  31.         <property name="dataBinding" ref="aegisBean" />  
  32.     </bean>  
  33.   
  34.     <bean id="aegisBean"  
  35.         class="org.apache.cxf.aegis.databinding.AegisDatabinding" />  
  36. </beans>  



    第三步:服务端程序编写了,打包部署 

    客户端开发 
    第一步:WebService客户端生成,可以手工编写。这里我就通过配置Ant文件来生成WebService客户端:

Java代码    收藏代码
  1. <?xml version="1.0"?>  
  2. <project name="cxf" basedir="." default="usage">  
  3.     <property name="webroot.dir" value="E:/workspace3/CXFSpring" />  
  4.     <!-- webservice接口 -->  
  5.     <property name="target.class" value="com.itdcl.service.IService" />  
  6.     <property name="wsdl.name" value="Service.wsdl" />  
  7.     <property name="src.dir" value="src" />  
  8.     <property name="client.dir" value="client" />  
  9.     <property name="server.dir" value="server" />  
  10.     <property name="compile.dir" value="${webroot.dir}/bin" />  
  11.   
  12.     <target name="usage">  
  13.         <echo  
  14.             message="java2wsdl         -->java文件生成wsdl文件,java2wsdl -Ddir=com/starit/cxf/java2wsdl -Dsrc=com.starit.cxf.java2wsdl.Hello" />  
  15.         <echo  
  16.             message="wsdl2javaClient       -->java文件生成client,java2wsdl -Ddir=com/starit/cxf/java2wsdl -Dwsdl=hello.wsdl" />  
  17.     </target>  
  18.     <target name="java2wsdl">  
  19.         <echo message="生成wsdl文件" />  
  20.         <exec dir="${compile.dir}" executable="cmd.exe">  
  21.             <arg line="/c java2ws" />  
  22.             <arg line="-o ${wsdl.name} -wsdl ${target.class}" />  
  23.         </exec>  
  24.         <!-- copy资源文件 -->  
  25.         <move todir="${src.dir}" preservelastmodified="true">  
  26.             <fileset dir="${compile.dir}">  
  27.                 <include name="**/*.wsdl" />  
  28.             </fileset>  
  29.         </move>  
  30.     </target>  
  31.   
  32.     <target name="wsdl2javaClient">  
  33.         <echo message="生成java client文件" />  
  34.         <mkdir dir="${client.dir}" />  
  35.         <exec dir="." executable="cmd.exe">  
  36.             <arg line="/c wsdl2java" />  
  37.             <!-- 生成的java所存目录 -->  
  38.             <arg line="-d ${client.dir}"/>  
  39.             <!-- wsdl文件所在路径 -->  
  40.             <arg line="-client src\*.wsdl" />  
  41.         </exec>  
  42.     </target>  
  43. </project>  



    第二步:WebService接口注册配置

Java代码    收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.     xsi:schemaLocation="  
  6.         http://www.springframework.org/schema/beans   
  7.         http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  8.         http://cxf.apache.org/jaxws   
  9.         http://cxf.apache.org/schemas/jaxws.xsd">  
  10.   
  11.     <!--  jaxws:client id="helloWorldClient"  
  12.         address="http://localhost:9999/cxf/HelloWorld"  
  13.         serviceClass="com.itdcl.service.IHelloWorld" />-->  
  14.   
  15.     <!--  jaxws:client id="myTestClient"  
  16.         address="http://localhost:9999/cxf/MyTest"  
  17.         serviceClass="com.itdcl.service.IMyTest" />-->  
  18.   
  19.     <jaxws:client id="service"  
  20.         address="http://localhost:9999/cxf/Service"  
  21.         serviceClass="com.itdcl.service.IService" />  
  22. </beans>  


     
    剩下的事就测试我们整合是否成功了,编写一个JUnit测试类或写一个Main函数测试一下。

Java代码    收藏代码
  1. public class JaxbSampleClient {  
  2.     private final static String MODEL = "com.itdcl.model";  
  3.     public static void main(String[] args) throws FileNotFoundException, JAXBException, ParserConfigurationException, TransformerException {  
  4.         ApplicationContext context = new ClassPathXmlApplicationContext(  
  5.                 "beans.xml");  
  6.         IService service = (IService) context.getBean("service");  
  7.           
  8.           
  9.         ObjectFactory factory = new ObjectFactory();  
  10.         Customer customer = factory.createCustomer();  
  11.         customer.setId(1);  
  12.         customer.setAge("26");  
  13.         customer.setName("Josen");  
  14.         customer.setAddress(100);  
  15.   
  16.         Customers customers = factory.createCustomers();  
  17.         customers.setCustomer(customer);  
  18.   
  19.         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
  20.         dbf.setNamespaceAware(true);  
  21.         DocumentBuilder db = dbf.newDocumentBuilder();  
  22.         Document doc = db.newDocument();  
  23.   
  24.         // 转换  
  25.         JAXBContext jaxbContext = JAXBContext.newInstance(MODEL);  
  26.         Marshaller marshaller = jaxbContext.createMarshaller();  
  27.         //将customers数据与doc绑定  
  28.         marshaller.marshal(customers, doc);  
  29.           
  30.         DOMSource domSource = new DOMSource(doc);  
  31.         StringWriter writer = new StringWriter();  
  32.         StreamResult result = new StreamResult(writer);  
  33.         TransformerFactory tf = TransformerFactory.newInstance();  
  34.         Transformer transformer = tf.newTransformer();  
  35.         transformer.transform(domSource, result);  
  36.         String xmlString = writer.toString();  
  37.         //System.out.println(xmlString);  
  38.           
  39.         service.update(xmlString);  
  40.     }  
  41.   
  42. }  



    运行一下,看插入数据库没有。这节到此就靠一段落啦,下节讲解WS-Security

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值