Spring3+Hibernate4整合(一):基本框架搭建


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

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

整合的第一步将Jar引入到工程里面来,引入之后更改配置项目配置。下面是项目的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">
    <!-- log4j 配置  开始 -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>    
        <param-value>
            /WEB-INF/classes/com/avicit/resource/log4j/log4j.properties
        </param-value>
    </context-param>
    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>600000</param-value>
    </context-param>
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>fes.root</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <!-- log4j 配置  结束 -->

    <!-- 设置servlet编码开始 -->
    <filter>
        <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!-- 设置servlet编码结束 -->
    
    <!-- 设置BackURL开始 -->    
    <filter>
        <filter-name>BackURLFilter</filter-name>
        <filter-class>com.avicit.framework.web.filter.BackURLFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>BackURLFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 设置BackURL结束 -->    
    
    <!-- Spring配置文件开始  -->    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:com/avicit/resource/spring/spring-base.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <!-- Spring配置文件结束 -->
        
    <filter>
        <filter-name>openSessionInVieFilter</filter-name>
        <filter-class>
            org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>openSessionInVieFilter</filter-name>
        <servlet-name>spring</servlet-name>
    </filter-mapping>

    <!-- 浏览器不支持put,delete等method,
    由该filter将/blog?_method=delete转换为标准的http delete方法 -->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <servlet-name>spring</servlet-name>
    </filter-mapping>


    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
           <init-param>      
               <param-name>contextConfigLocation</param-name>      
               <param-value>classpath*:com/avicit/resource/spring/spring-dispather.xml
               </param-value>
            </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping> 
     
    <error-page>    
        <error-code>500</error-code>    
        <location>/error.jsp?code=500</location>  
     </error-page>  
     
     <error-page>    
         <error-code>404</error-code>    
         <location>/error.jsp?code=404</location>  
     </error-page>  
     
     <error-page>    
         <error-code>405</error-code>    
         <location>/error.jsp?code=405</location>  
     </error-page>  
     
     <error-page>    
         <error-code>406</error-code>    
         <location>/error.jsp?code=406</location>  
     </error-page>  
     
     <error-page>    
         <error-code>415</error-code>    
         <location>/error.jsp?code=415</location>  
     </error-page> 
     
     <error-page>    
         <error-code>400</error-code>    
         <location>/error.jsp?code=400</location>  
     </error-page>

    <welcome-file-list>
        <welcome-file>/index</welcome-file>
    </welcome-file-list></web-app>

 
 其实Spring的配置跟以前没多大区别,关键就是设置Spring的启动监听器和Spring配置文件的地址,下面是spring-base.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.1.xsd
       ">

    <!-- 扫描注解Bean -->
    <context:component-scan base-package="com.avicit">
        <context:exclude-filter type="annotation" 
        expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <bean id="propertyConfigurer" 
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
            	<value>classpath*:com/avicit/resource/jdbc/jdbc.properties</value>
            	<value>classpath*:com/avicit/resource/hibernate/hibernate.properties</value>
            </list>
        </property>
    </bean>

 
    <!-- 国际化的消息资源文件 -->
    <bean id="messageSource" 
         class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <!-- 在web环境中一定要定位到classpath 否则默认到当前web应用下找  -->
                <value>classpath:com/avicit/resource/message/messages</value>
            </list>
        </property>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="cacheSeconds" value="60"/>
    </bean>
    
    <import resource="classpath*:com/avicit/resource/spring/spring-dao.xml"/></beans>

 
 这一段配置也没有什么特别地方,加载jdbc.properties数据库配置和hiberate.properties配置文件、

设置扫描Annotation注册Bean的包,但是下面有段配置可能不是很熟悉:      

<context:component-scan base-package="com.avicit">
        <context:exclude-filter type="annotation" 
        expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

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

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

<servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
           <init-param>      
               <param-name>contextConfigLocation</param-name>      
               <param-value>
                   classpath*:com/avicit/resource/spring/spring-dispather.xml
               </param-value>
           </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

 
 

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

<beans xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p"  
xmlns:context="http://www.springframework.org/schema/context"  
xmlns:util="http://www.springframework.org/schema/util" 
xmlns:mvc="http://www.springframework.org/schema/mvc"  
xsi:schemaLocation="
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">  
       <!-- 会自动注册了validator ConversionService -->  
 <mvc:annotation-driven validator="validator"   
        conversion-service="conversion-service" />  
        <!-- 以下 validator ConversionService 在使用 mvc:annotation-driven 会 自动注册 -->
 <bean id="validator"    
     class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">    
  <property name="providerClass" value="org.hibernate.validator.HibernateValidator" />    
  <!-- 如果不加默认到 使用classpath下的 ValidationMessages.properties -->    
  <property name="validationMessageSource" ref="messageSource" /> 
 </bean>  
 <bean id="conversion-service"    
     class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />  
     <!-- 开启controller注解支持 -->  
     <!-- 注:如果base-package=com.avicit 则注解事务不起作用 TODO 读源码 -->  
 <context:component-scan base-package="com.avicit">    
     <context:include-filter type="annotation"      
         expression="org.springframework.stereotype.Controller" />    
     <context:exclude-filter type="annotation"      
         expression="org.springframework.stereotype.Service" />  
 </context:component-scan>  
 
 <mvc:resources mapping="/**" location="/" />  
 <mvc:interceptors>    
 <mvc:interceptor>      
 <mvc:mapping path="/*" />      
 <bean        
    class="com.avicit.framework.interceptor.dispatcher.HandlerDispatcherContextInterceptor">
 </bean>    
 </mvc:interceptor>
     
 <mvc:interceptor>      
 <mvc:mapping path="/*" />      
 <bean        
     class="com.avicit.framework.interceptor.pagination.HandlerPaginationInterceptor">
     </bean>    
 </mvc:interceptor>  
 </mvc:interceptors>  
 
 <mvc:view-controller path="/" view-name="forward:/index" />  
 <!-- 当在web.xml 中 DispatcherServlet使用 <url-pattern>/</url-pattern> 映射时,
 能映射静态资源 -->  
 <bean    
     class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
  <bean id="handlerAdapter"   
      class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
 </bean>  
 
 <bean    
     class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">  
       
 <property name="mediaTypes">      
 <map>        
     <entry key="json" value="application/json" />        
     <entry key="xml" value="application/xml" />        
     <entry key="html" value="text/html" />     
 </map>    
 </property>    
 
 <property name="viewResolvers">      
     <list>        
         <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />        
         <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">          
         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
         <property name="prefix" value="/" />          
         <property name="suffix" value=".jsp" />        
         </bean>      
     </list>    
 </property>  
</bean>  

<!-- 控制器异常处理 -->  
<bean id="exceptionResolver"    
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">    
    <property name="exceptionMappings">     
         <props>       
              <prop key="java.lang.Exception">          error        </prop>      
         </props>   
    </property>  
</bean>

</beans>

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

<!-- 开启controller注解支持 -->  <!-- 注:如果base-package=com.avicit 则注解事务不起作用 
TODO 读源码 -->  

<context:component-scan base-package="com.avicit">   
     <context:include-filter type="annotation"      
         expression="org.springframework.stereotype.Controller" />    
     <context:exclude-filter type="annotation"      
     expression="org.springframework.stereotype.Service" />  
</context:component-scan>

 

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

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  x
        mlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:p="http://www.springframework.org/schema/p"  
        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"> 
         
    <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">    
        <property name="alias" value="proxoolDataSource" />    
        <property name="driver" value="${jdbc.driver}" />    
        <property name="driverUrl" value="${jdbc.url}" />    
        <property name="user" value="${jdbc.username}" />    
        <property name="password" value="${jdbc.password}" />    
        <property name="maximumConnectionCount" value="${jdbc.maximum.connection.count}" />
        <property name="minimumConnectionCount" value="${jdbc.minimum.connection.count}" />
        <property name="statistics" value="${jdbc.statistics}" />    
        <property name="simultaneousBuildThrottle" v
            alue="${jdbc.simultaneous.build.throttle}" />  
     </bean>  
     
     <bean id="sessionFactory"    
             class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">    
          <property name="dataSource" ref="dataSource" />    
          <property name="packagesToScan" value="com.avicit" />    
          <property name="hibernateProperties">      
              <props>        
                  <prop key="hibernate.dialect">${hibernate.dialect}</prop>        
                  <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>        
                  <prop key="hibernate.format_sql">true</prop>       
                  <prop key="hibernate.query.substitutions">
                          ${hibernate.query.substitutions}</prop>        
                  <prop key="hibernate.default_batch_fetch_size">
                          ${hibernate.default_batch_fetch_size}</prop>        
                  <prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>
                  <prop key="hibernate.generate_statistics">
                          ${hibernate.generate_statistics}</prop>        
                  <prop key="hibernate.bytecode.use_reflection_optimizer">
                          ${hibernate.bytecode.use_reflection_optimizer}</prop>        
                  <prop key="hibernate.cache.use_second_level_cache">
                          ${hibernate.cache.use_second_level_cache}</prop>        
                  <prop key="hibernate.cache.use_query_cache">
                          ${hibernate.cache.use_query_cache}</prop>        
                  <prop key="hibernate.cache.region.factory_class">
                          ${hibernate.cache.region.factory_class}</prop>        
                  <prop key="net.sf.ehcache.configurationResourceName">
                          ${net.sf.ehcache.configurationResourceName}</prop>        
                  <prop key="hibernate.cache.use_structured_entries">
                          ${hibernate.cache.use_structured_entries}</prop>      
                  </props>    
            </property>
       </bean>  
       
       <bean id="lookupResolver" 
           class="com.avicit.framework.support.matchrule.context.HibernateMatchRuleResolver"> 
          <property name="packagesToScan" value="com.avicit.fes.*" />  
       </bean>  
       
       <!-- 开启AOP监听 只对当前配置文件有效 -->  
       <aop:aspectj-autoproxy expose-proxy="true" />  
       <!-- 开启注解事务 只对当前配置文件有效 -->  
       <tx:annotation-driven transaction-manager="txManager" />  
       <bean id="txManager"    
           class="org.springframework.orm.hibernate4.HibernateTransactionManager">    
           <property name="sessionFactory" ref="sessionFactory" />  
       </bean>  
       <tx:advice id="txAdvice" transaction-manager="txManager">    
       <tx:attributes>      
       <tx:method name="save*" propagation="REQUIRED" />      
       <tx:method name="add*" propagation="REQUIRED" />      
       <tx:method name="create*" propagation="REQUIRED" />      
       <tx:method name="insert*" propagation="REQUIRED" />      
       <tx:method name="update*" propagation="REQUIRED" />      
       <tx:method name="merge*" propagation="REQUIRED" />      
       <tx:method name="del*" propagation="REQUIRED" />      
       <tx:method name="remove*" propagation="REQUIRED" />      
       <tx:method name="put*" propagation="REQUIRED" />      
       <tx:method name="use*" propagation="REQUIRED" />      
       <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到 -->      
       <tx:method name="get*" propagation="REQUIRED" read-only="true" />      
       <tx:method name="count*" propagation="REQUIRED" read-only="true" />      
       <tx:method name="find*" propagation="REQUIRED" read-only="true" />      
       <tx:method name="list*" propagation="REQUIRED" read-only="true" />      
       <tx:method name="*" read-only="true" />    
       </tx:attributes>  
       </tx:advice>  
       
       <aop:config expose-proxy="true">    
       <!-- 只对业务逻辑层实施事务 -->    
       <aop:pointcut id="txPointcut"      
           expression="execution(* com.avicit..service..*.*(..))" />    
           <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" /> 
        </aop:config>
  </beans>

 
 

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

Spring3+Hibernate4的框架整合差不多,后面会写如何实现SpringMVC整合Ext,那才是更有意思的部分。


你可能感兴趣的:(Spring3+Hibernate4整合(一):基本框架搭建)