首页 > 编程语言 > 详细

搭建基于spring MVC框架 + RESTful架构风格技术总结

时间:2015-11-15 23:29:58      阅读:561      评论:0      收藏:0      [点我收藏+]
实战篇:
在SpringMVC框架中搭建RESTful架构风格来完成客户端与服务器端的低耦合度、可扩展性、高并发与大数据流量的访问。
用RESTful架构的创建步骤:
1.创建一个全新的Web工程
2.导包,导入所需要的所有第三方jar包。(springMVC+Hibernate的基本包是必须的)
3.作配置,针对不同的项目需求和不同的搭建设计,开发人员可以按照自己的编码风格来设计符合项目开发具体
应该用多少篇配置文件。但是这几篇配置文件是必不可少的:
3-1.web.xml配置文件:最基本的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>epetrestful</display-name>
    <servlet>
      <servlet-name>springMVCReSTful</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:config/spring/spring-action.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>    
    </servlet>
    <servlet-mapping>
      <servlet-name>springMVCReSTful</servlet-name>
      <url-pattern>/</url-pattern><!-- 直接通过/去匹配路径 -->
    </servlet-mapping>
<!-- 如果有乱码我们则需要配置字符编码集的过滤器来防止乱码问题 -->
    <filter>
      <filter-name>encodingFilter</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
      </init-param>
      <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
      </init-param>
    </filter>
     
    <filter-mapping>
      <filter-name>encodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>login.html</welcome-file>
  </welcome-file-list>
</web-app>
注:
3-2.配置spring-commoms.xml文件。(要注意我们需要将连接数据库资源的信息用一篇外部的database.prpertise的属性文件来
具体的配置到该spring-commoms.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-3.0.xsd
                     http://www.springframework.org/schema/context
                     http://www.springframework.org/schema/context/spring-context-3.0.xsd
                     http://www.springframework.org/schema/aop
                     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                     http://www.springframework.org/schema/tx
                     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
    default-autowire="byName">
    <!-- 告知外部的database.propertise文件到spring容器中 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="location"></property><!--这里name属性的值必须是location,它是在 
      PropertyPlaceholderConfigurer类中调用了这个location的属性-->
        <list>
          <value>classpath:database.prppertise</value><!-- 绑定外部写的database.prppertise属性文件的路径 -->
        </list>
    </bean>
<!-- 配置数据源的驱动连接,这样配置的优势在于:效率得到了提高,具有pool池,我们在进行增删
                          改查时就不用每次都要连接数据库这样的一个操作。 -->
    <bean id="dataSoure" class="org.apache.commons.dbcp.BasicDataSource" >
      <property name="driverClassName">
       <value>${driverClassName}</value>
      </property>
      <property name="url">
         <value>${url}</value>
      </property>
      <property name="username">
        <value>${username}</value>
      </property>
      <property name="password">
        <value>${password}</value>
      </property>   
    </bean>
 
<!-- 告知Hibernate的sessionFactory -->    
    <bean id="sessionFactory" 
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSoure"></property>      
    <property name="hibernateProperties"></property>
    <props key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</props> 
    <props key="hibernate.show_sql">true</props>
    <props key="hibernate.format_sql">true</props>
    <property name="packagesToScan">
      <list>
         <value>com.lh.model</value>
      </list>
    </property>
    </bean>
    <!-- 事物管理器的配置 -->
    <bean id="transactionManager" 
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
       <property name="sessionFactory" ref="sessionFactory"></property>   
    </bean>
<!-- 扫描Spring注解的配置 -->
<context:component-scan base-package="com.lh"></context:component-scan>
<!-- 添加注解事物支持的配置 -->    
<tx:annotation-driven transaction-manager="transationManager"></tx:annotation-driven>
</beans>
 
3-3.配置一篇spring-action.xml文件(其中在该配置文件中需要将上面的spring-commoms.xml的配置
文件导入到其间,这里体现了在轻量级的spring容器中spring MVC框架是包含在spring容器之中的。)
基本配置如下:
<?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-3.0.xsd
                     http://www.springframework.org/schema/context
                     http://www.springframework.org/schema/context/spring-context-3.0.xsd
                     http://www.springframework.org/schema/aop
                     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                     http://www.springframework.org/schema/tx
                     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
                     default-autowire="byName"
                     >
  <!--导入外部的spring-commons.xml的配置文件 -->       
  <import resource="classpath:config/spring/spring-commons.xml"/>
  <!--配置spring MVC的注解 驱动 -->
  <mvc:annotation-driven/>
  <!--配置静态文件  -->      
 <mvc:default-servlet-handler/>
</beans>
故,综上所述:在一个用spring MVC框架来实现RESTful架构风格的互联网终端接口至少都需要3篇
或3篇以上的配置文件(关键看程序员自己的风格来决定)。
4.设计页面(可用html,jsp)
5.书写model类(表现层),因为后端的开发用到了Hibernate框架的(ORM映射)对象关系映射技术,故,model的对象属性要与数据库表的字段
相对应,于此,才能达到关系数据库和面向对象之间的映射(即采用hibernate的注解形式将关系和对象进行绑定)。
6.书写dao(数据访问层)和daoImpl(接口实现类)。
7.书写service(业务层)和serviceImpl(接口实现类)。
8.书写单元测试,进行校验功能是否满足要求。
9.重要环节:导入我们在开发过程中所需要使用的所有js,css,jQuery,在一个Web项目中他们都应该放入WebRoot的同级目录下。

搭建基于spring MVC框架 + RESTful架构风格技术总结

原文:http://my.oschina.net/lwhmdj0823/blog/530638

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!