Spring3+Hibernate4+SpringMVC整合Ext:项目架构搭建

来源:互联网 发布:厄米特矩阵怎么求 编辑:程序博客网 时间:2024/04/29 17:35

前言

       前段时间突然想用SpringMVC结合Ext做一个框架原型,整合后发现SpringMVC配合Ext简直天衣无缝,当然SpringMVC结合别的UI框架应该也是天衣无缝的。SpringMVC比Struts2确实要强大很多,特别对于Ext框架JSON数据的完美支撑,开发起来相当舒服。Spring3整合Hibernate4的时候可能有点问题,跟Spring2+Hibernate3有很大的区别,区别在于Hibernate4实现了对事务的管理,所以Spring针对Hibernate4就没有提供HibernateDaoSupport这个类。

       整合有个原则是分框架的整合,比如我们先整合Spring、再整合SpringMVC接着整合Hibernate

   整合Spring

        整合的第一步将Jar引入到工程里面来,引入之后更改配置项目配置。下面是项目的web.xml文件的详细信息:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.     <!-- log4j 配置  开始 -->  
  7.     <context-param>  
  8.         <param-name>log4jConfigLocation</param-name>  
  9.         <param-value>/WEB-INF/classes/com/avicit/resource/log4j/log4j.properties</param-value>  
  10.     </context-param>  
  11.     <context-param>  
  12.         <param-name>log4jRefreshInterval</param-name>  
  13.         <param-value>600000</param-value>  
  14.     </context-param>  
  15.     <context-param>  
  16.         <param-name>webAppRootKey</param-name>  
  17.         <param-value>fes.root</param-value>  
  18.     </context-param>  
  19.     <listener>  
  20.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  21.     </listener>  
  22.     <!-- log4j 配置  结束 -->  
  23.   
  24.     <!-- 设置servlet编码开始 -->  
  25.     <filter>  
  26.         <filter-name>CharacterEncodingFilter</filter-name>  
  27.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  28.         <init-param>  
  29.             <param-name>encoding</param-name>  
  30.             <param-value>utf-8</param-value>  
  31.         </init-param>  
  32.         <init-param>  
  33.             <param-name>forceEncoding</param-name>  
  34.             <param-value>true</param-value>  
  35.         </init-param>  
  36.     </filter>  
  37.   
  38.     <filter-mapping>  
  39.         <filter-name>CharacterEncodingFilter</filter-name>  
  40.         <url-pattern>/*</url-pattern>  
  41.     </filter-mapping>  
  42.       
  43.     <!-- 设置servlet编码结束 -->  
  44.       
  45.     <!-- 设置BackURL开始 -->      
  46.     <filter>  
  47.         <filter-name>BackURLFilter</filter-name>  
  48.         <filter-class>com.avicit.framework.web.filter.BackURLFilter</filter-class>  
  49.     </filter>  
  50.   
  51.     <filter-mapping>  
  52.         <filter-name>BackURLFilter</filter-name>  
  53.         <url-pattern>/*</url-pattern>  
  54.     </filter-mapping>  
  55.     <!-- 设置BackURL结束 -->      
  56.       
  57.     <!-- Spring配置文件开始  -->      
  58.     <context-param>  
  59.         <param-name>contextConfigLocation</param-name>  
  60.         <param-value>classpath*:com/avicit/resource/spring/spring-base.xml</param-value>  
  61.     </context-param>  
  62.   
  63.     <listener>  
  64.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  65.     </listener>  
  66.     <!-- Spring配置文件结束 -->  
  67.           
  68.     <filter>  
  69.         <filter-name>openSessionInVieFilter</filter-name>  
  70.         <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>  
  71.     </filter>  
  72.     <filter-mapping>  
  73.         <filter-name>openSessionInVieFilter</filter-name>  
  74.         <servlet-name>spring</servlet-name>  
  75.     </filter-mapping>  
  76.   
  77.     <!-- 浏览器不支持put,delete等method,由该filter将/blog?_method=delete转换为标准的http delete方法 -->  
  78.     <filter>  
  79.         <filter-name>HiddenHttpMethodFilter</filter-name>  
  80.         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>  
  81.     </filter>  
  82.     <filter-mapping>  
  83.         <filter-name>HiddenHttpMethodFilter</filter-name>  
  84.         <servlet-name>spring</servlet-name>  
  85.     </filter-mapping>  
  86.   
  87.   
  88.     <servlet>  
  89.         <servlet-name>spring-dispatcher</servlet-name>  
  90.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  91.            <init-param>  
  92.             <param-name>contextConfigLocation</param-name>  
  93.             <param-value>classpath*:com/avicit/resource/spring/spring-dispather.xml</param-value>  
  94.         </init-param>  
  95.         <load-on-startup>1</load-on-startup>  
  96.     </servlet>  
  97.   
  98.     <servlet-mapping>  
  99.         <servlet-name>spring-dispatcher</servlet-name>  
  100.         <url-pattern>/</url-pattern>  
  101.     </servlet-mapping>  
  102.     <error-page>  
  103.         <error-code>500</error-code>  
  104.         <location>/error.jsp?code=500</location>  
  105.     </error-page>  
  106.   
  107.     <error-page>  
  108.         <error-code>404</error-code>  
  109.         <location>/error.jsp?code=404</location>  
  110.     </error-page>  
  111.     <error-page>  
  112.         <error-code>405</error-code>  
  113.         <location>/error.jsp?code=405</location>  
  114.     </error-page>  
  115.     <error-page>  
  116.         <error-code>406</error-code>  
  117.         <location>/error.jsp?code=406</location>  
  118.     </error-page>  
  119.     <error-page>  
  120.         <error-code>415</error-code>  
  121.         <location>/error.jsp?code=415</location>  
  122.     </error-page>  
  123.     <error-page>  
  124.         <error-code>400</error-code>  
  125.         <location>/error.jsp?code=400</location>  
  126.     </error-page>  
  127.   
  128.     <welcome-file-list>  
  129.         <welcome-file>/index</welcome-file>  
  130.     </welcome-file-list>  
  131.   
  132. </web-app>  

其实Spring的配置跟以前没多大区别,关键就是设置Spring的启动监听器和Spring配置文件的地址,下面是spring-base.xml的内容:

[html] view plaincopyprint?
  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:context="http://www.springframework.org/schema/context"  
  7.        xsi:schemaLocation="  
  8.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  9.        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
  10.        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
  11.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  12.        ">  
  13.   
  14.     <!-- 扫描注解Bean -->  
  15.     <context:component-scan base-package="com.avicit">  
  16.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  17.     </context:component-scan>  
  18.   
  19.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  20.         <property name="locations">  
  21.             <list>  
  22.                 <value>classpath*:com/avicit/resource/jdbc/jdbc.properties</value>  
  23.                 <value>classpath*:com/avicit/resource/hibernate/hibernate.properties</value>  
  24.             </list>  
  25.         </property>  
  26.     </bean>  
  27.   
  28.    
  29.     <!-- 国际化的消息资源文件 -->  
  30.     <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">  
  31.         <property name="basenames">  
  32.             <list>  
  33.                 <!-- 在web环境中一定要定位到classpath 否则默认到当前web应用下找  -->  
  34.                 <value>classpath:com/avicit/resource/message/messages</value>  
  35.             </list>  
  36.         </property>  
  37.         <property name="defaultEncoding" value="UTF-8"/>  
  38.         <property name="cacheSeconds" value="60"/>  
  39.     </bean>  
  40.       
  41.     <import resource="classpath*:com/avicit/resource/spring/spring-dao.xml"/>  
  42.   
  43. </beans>  

这一段配置也没有什么特别地方,加载jdbc.properties数据库配置和hiberate.properties配置文件、设置扫描Annotation注册Bean的包,但是下面有段配置可能不是很熟悉:

[html] view plaincopyprint?
  1. <context:component-scan base-package="com.avicit">  
  2.     <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  3. </context:component-scan>  

这里设置了不扫描的Annotation的类型,这是因为org.springframework.stereotype.Controller是SpringMVC的控制器的注解,使用这个注解注册的Bean在SpringMVC容器启动的时候已经实例化了,所以在Spring容器里面就不需要进行实例化了。

   整合SpringMVC

在web.xml文件的配置中可以看到这么一段配置:

[html] view plaincopyprint?
  1.  <servlet>  
  2.      <servlet-name>spring-dispatcher</servlet-name>  
  3.      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  4.         <init-param>  
  5. <param-name>contextConfigLocation</param-name>  
  6. <param-value>classpath*:com/avicit/resource/spring/spring-dispather.xml</param-value>  
  7.      </init-param>  
  8.      <load-on-startup>1</load-on-startup>  
  9.  </servlet>  
  10.   
  11.  <servlet-mapping>  
  12.      <servlet-name>spring-dispatcher</servlet-name>  
  13.      <url-pattern>/</url-pattern>  
  14.  </servlet-mapping>  

这个就是SpringMVC的配置,配置SpringMVC的容器也可以说是调度器,下面看下spring-dispather.xml中的配置:

[html] view plaincopyprint?
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.     xsi:schemaLocation="  
  6.         http://www.springframework.org/schema/util  
  7.         http://www.springframework.org/schema/util/spring-util-3.1.xsd  
  8.         http://www.springframework.org/schema/beans   
  9.         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  10.         http://www.springframework.org/schema/context   
  11.         http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  12.         http://www.springframework.org/schema/mvc  
  13.        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">  
  14.   
  15.     <!-- 会自动注册了validator ConversionService -->  
  16.     <mvc:annotation-driven validator="validator"  
  17.         conversion-service="conversion-service" />  
  18.   
  19.     <!-- 以下 validator ConversionService 在使用 mvc:annotation-driven 会 自动注册 -->  
  20.     <bean id="validator"  
  21.         class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">  
  22.         <property name="providerClass" value="org.hibernate.validator.HibernateValidator" />  
  23.         <!-- 如果不加默认到 使用classpath下的 ValidationMessages.properties -->  
  24.         <property name="validationMessageSource" ref="messageSource" />  
  25.     </bean>  
  26.     <bean id="conversion-service"  
  27.         class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />  
  28.   
  29.   
  30.     <!-- 开启controller注解支持 -->  
  31.     <!-- 注:如果base-package=com.avicit 则注解事务不起作用 TODO 读源码 -->  
  32.     <context:component-scan base-package="com.avicit">  
  33.         <context:include-filter type="annotation"  
  34.             expression="org.springframework.stereotype.Controller" />  
  35.         <context:exclude-filter type="annotation"  
  36.             expression="org.springframework.stereotype.Service" />  
  37.     </context:component-scan>  
  38.   
  39.     <mvc:resources mapping="/**" location="/" />  
  40.     <mvc:interceptors>  
  41.         <mvc:interceptor>  
  42.             <mvc:mapping path="/*" />  
  43.             <bean  
  44.                 class="com.avicit.framework.interceptor.dispatcher.HandlerDispatcherContextInterceptor"></bean>  
  45.         </mvc:interceptor>  
  46.         <mvc:interceptor>  
  47.             <mvc:mapping path="/*" />  
  48.             <bean  
  49.                 class="com.avicit.framework.interceptor.pagination.HandlerPaginationInterceptor"></bean>  
  50.         </mvc:interceptor>  
  51.     </mvc:interceptors>  
  52.   
  53.     <mvc:view-controller path="/" view-name="forward:/index" />  
  54.     <!-- 当在web.xml 中 DispatcherServlet使用 <url-pattern>/</url-pattern> 映射时,能映射静态资源 -->  
  55.   
  56.     <bean  
  57.         class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  
  58.   
  59.     <bean id="handlerAdapter"  
  60.         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  61.     </bean>  
  62.   
  63.   
  64.     <bean  
  65.         class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">  
  66.         <property name="mediaTypes">  
  67.             <map>  
  68.                 <entry key="json" value="application/json" />  
  69.                 <entry key="xml" value="application/xml" />  
  70.                 <entry key="html" value="text/html" />  
  71.             </map>  
  72.         </property>  
  73.         <property name="viewResolvers">  
  74.             <list>  
  75.                 <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />  
  76.                 <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">  
  77.                     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />  
  78.                     <property name="prefix" value="/" />  
  79.                     <property name="suffix" value=".jsp" />  
  80.                 </bean>  
  81.             </list>  
  82.         </property>  
  83.     </bean>  
  84.   
  85.     <!-- 控制器异常处理 -->  
  86.     <bean id="exceptionResolver"  
  87.         class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
  88.         <property name="exceptionMappings">  
  89.             <props>  
  90.                 <prop key="java.lang.Exception">  
  91.                     error  
  92.                 </prop>  
  93.             </props>  
  94.         </property>  
  95.     </bean>  
  96.   
  97. </beans>  

这一部分配置是依据官方来的文档来的,大家看看文档就可以明白这段配置,在这里就不赘述了。但是这里有很关键的一处配置是官方文档没有提到的,也是整合Hiberate4中关键的配置,如果没有配置Hibernate肯定跑不起来,这段配置:

[html] view plaincopyprint?
  1. <!-- 开启controller注解支持 -->  
  2. <!-- 注:如果base-package=com.avicit 则注解事务不起作用 TODO 读源码 -->  
  3. <context:component-scan base-package="com.avicit">  
  4.     <context:include-filter type="annotation"  
  5.         expression="org.springframework.stereotype.Controller" />  
  6.     <context:exclude-filter type="annotation"  
  7.         expression="org.springframework.stereotype.Service" />  
  8. </context:component-scan>  

这里配置了扫描Controller,通过Controller注解注册的Bean是SpringMVC的控制器,但是为什么要排除Service注解呢?这是因为通过Service注册的Bean是要进行事务处理的。要生成动态代理进行事务控制,所以如果不排除的话,Service注册的Bean是不带事务处理的。所以在整合Hibernate的时候就会报没有事务的异常。

   整合Hibernate

Hibernate在Spring中如何配置,也就是spring-dao.xml:

[html] view plaincopyprint?
  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" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.   
  7.     xsi:schemaLocation="  
  8.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  9.         http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  10.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  11.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  
  12.   
  13.     <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">  
  14.         <property name="alias" value="proxoolDataSource" />  
  15.         <property name="driver" value="${jdbc.driver}" />  
  16.         <property name="driverUrl" value="${jdbc.url}" />  
  17.         <property name="user" value="${jdbc.username}" />  
  18.         <property name="password" value="${jdbc.password}" />  
  19.         <property name="maximumConnectionCount" value="${jdbc.maximum.connection.count}" />  
  20.         <property name="minimumConnectionCount" value="${jdbc.minimum.connection.count}" />  
  21.         <property name="statistics" value="${jdbc.statistics}" />  
  22.         <property name="simultaneousBuildThrottle" value="${jdbc.simultaneous.build.throttle}" />  
  23.     </bean>  
  24.   
  25.     <bean id="sessionFactory"  
  26.         class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  27.         <property name="dataSource" ref="dataSource" />  
  28.         <property name="packagesToScan" value="com.avicit" />  
  29.         <property name="hibernateProperties">  
  30.             <props>  
  31.                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>  
  32.                 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>  
  33.                 <prop key="hibernate.format_sql">true</prop>  
  34.                 <prop key="hibernate.query.substitutions">${hibernate.query.substitutions}</prop>  
  35.                 <prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop>  
  36.                 <prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>  
  37.                 <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>  
  38.                 <prop key="hibernate.bytecode.use_reflection_optimizer">${hibernate.bytecode.use_reflection_optimizer}</prop>  
  39.   
  40.                 <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>  
  41.                 <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>  
  42.                 <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>  
  43.                 <prop key="net.sf.ehcache.configurationResourceName">${net.sf.ehcache.configurationResourceName}</prop>  
  44.                 <prop key="hibernate.cache.use_structured_entries">${hibernate.cache.use_structured_entries}</prop>  
  45.             </props>  
  46.         </property>  
  47.     </bean>  
  48.   
  49.     <bean id="lookupResolver" class="com.avicit.framework.support.matchrule.context.HibernateMatchRuleResolver">  
  50.         <property name="packagesToScan" value="com.avicit.fes.*" />  
  51.     </bean>  
  52.   
  53.     <!-- 开启AOP监听 只对当前配置文件有效 -->  
  54.     <aop:aspectj-autoproxy expose-proxy="true" />  
  55.   
  56.     <!-- 开启注解事务 只对当前配置文件有效 -->  
  57.     <tx:annotation-driven transaction-manager="txManager" />  
  58.   
  59.     <bean id="txManager"  
  60.         class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
  61.         <property name="sessionFactory" ref="sessionFactory" />  
  62.     </bean>  
  63.   
  64.     <tx:advice id="txAdvice" transaction-manager="txManager">  
  65.         <tx:attributes>  
  66.             <tx:method name="save*" propagation="REQUIRED" />  
  67.             <tx:method name="add*" propagation="REQUIRED" />  
  68.             <tx:method name="create*" propagation="REQUIRED" />  
  69.             <tx:method name="insert*" propagation="REQUIRED" />  
  70.             <tx:method name="update*" propagation="REQUIRED" />  
  71.             <tx:method name="merge*" propagation="REQUIRED" />  
  72.             <tx:method name="del*" propagation="REQUIRED" />  
  73.             <tx:method name="remove*" propagation="REQUIRED" />  
  74.             <tx:method name="put*" propagation="REQUIRED" />  
  75.             <tx:method name="use*" propagation="REQUIRED" />  
  76.             <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到 -->  
  77.             <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
  78.             <tx:method name="count*" propagation="REQUIRED" read-only="true" />  
  79.             <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
  80.             <tx:method name="list*" propagation="REQUIRED" read-only="true" />  
  81.             <tx:method name="*" read-only="true" />  
  82.         </tx:attributes>  
  83.     </tx:advice>  
  84.     <aop:config expose-proxy="true">  
  85.         <!-- 只对业务逻辑层实施事务 -->  
  86.         <aop:pointcut id="txPointcut"  
  87.             expression="execution(* com.avicit..service..*.*(..))" />  
  88.         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />  
  89.     </aop:config>  
  90. </beans>  

Hibernate的配置跟Hibernate3没有很大的区别,唯一的区别就是所有的操作都必须开启事务。

   关于Ext的整合

Spring3+Hibernate4的框架整合差不多,后面会写如何实现SpringMVC整合Ext,Ext的Grid组件提供了RESTful方式的访问而SpringMVC也支持这种访问。如何处理对Ext的分页,如何返回json数据给Ext,那才是更有意思的部分。


   实例下载

http://download.csdn.net/detail/leecho571/4619860实例下载

2 0
原创粉丝点击