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


前言

       前段时间突然想用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文件的详细信息:

  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>  
<?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的内容:

  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>  
<?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的包,但是下面有段配置可能不是很熟悉:

  1. <context:component-scan base-package="com.avicit">  
  2.     <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  3. </context:component-scan>  
    <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容器里面就不需要进行实例化了。

   整合SpringMVC

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

  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>  
    <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中的配置:

  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>  
<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肯定跑不起来,这段配置:

  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注解支持 -->
	<!-- 注:如果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

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

  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>  
<?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: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" value="${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没有很大的区别,唯一的区别就是所有的操作都必须开启事务。

   关于Ext的整合

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


   实例下载

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

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: Spring MVC、Spring 3和Hibernate 4是常用的Java开发框架和库。你可以通过以下步骤来下载它们的jar包。 1. Spring MVC的jar包下载: 你可以在Spring官方网站(https://spring.io/)的"Projects"部分找到Spring MVC的相关信息和下载链接。在该页面上,你可以找到最新版本的Spring MVC并下载与之对应的jar包。 2. Spring 3的jar包下载: 同样,你可以在Spring官方网站上找到Spring 3的相关信息和下载链接。在该网站上,你可以找到Spring 3的最新版本并下载相应的jar包。 3. Hibernate 4的jar包下载: 你可以在Hibernate官方网站(https://hibernate.org)的"Downloads"部分找到Hibernate 4的相关信息和下载链接。在该页面上,你可以找到最新版本的Hibernate 4并下载对应的jar包。 无论是Spring MVC、Spring 3还是Hibernate 4,你可以选择下载官方提供的jar包来开始你的项目,也可以通过maven等构建工具来管理它们的依赖关系。在将这些jar包添加到你的项目中时,你应该将它们添加到你的构建路径下,并在项目配置文件中配置相应的引用和依赖。 希望以上的回答能够对你有所帮助! ### 回答2: 要下载Spring MVC,Spring 3以及Hibernate 4的jar包,有以下几种方法可供选择: 方法一:通过官方网站下载 你可以直接访问Spring官方网站(https://spring.io/)和Hibernate官方网站(https://hibernate.org/orm/)的下载页面,在这些页面上你可以找到相关的jar包。Spring官方网站提供了关于Spring MVC和Spring 3的jar包下载链接,Hibernate官方网站提供了关于Hibernate 4的jar包下载链接。 方法二:使用Maven依赖管理工具 如果你使用Maven作为项目构建工具,可以在项目的pom.xml文件中添加相关依赖,然后使用Maven自动下载这些jar包。在pom.xml文件中,你需要添加Spring MVC、Spring 3和Hibernate 4的相应依赖配置,然后运行Maven命令执行依赖下载。 方法三:使用Gradle依赖管理工具 如果你使用Gradle作为项目构建工具,可以在项目的build.gradle文件中添加相关依赖,然后使用Gradle自动下载这些jar包。在build.gradle文件中,你需要添加Spring MVC、Spring 3和Hibernate 4的相应依赖配置,然后运行Gradle命令执行依赖下载。 总之,你可以通过访问官方网站下载jar包,也可以使用Maven或Gradle工具自动下载。无论选择哪种方法,确保下载的jar包版本与你的项目要求的版本匹配,以确保项目能够正常运行。 ### 回答3: 可以在官方网站和第三方资源库下载Spring MVC、Spring 3和Hibernate 4的JAR包。以下是一些常用的下载渠道: 1. 官方网站下载:可以通过Spring官方网站(https://spring.io/projects/spring-framework)下载Spring MVC和Spring 3的JAR包。在网站上找到相应的版本并下载相关的JAR文件。 2. Maven下载:如果你使用Maven项目管理工具,你可以在pom.xml文件中添加相应的依赖关系,然后Maven会自动下载所需的JAR包。例如,你可以添加以下依赖项以下载Spring MVC、Spring 3和Hibernate 4的JAR包: ```xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.11.Final</version> </dependency> ``` 然后,执行Maven的更新命令以下载所需的依赖项。 3. 第三方资源库下载:除了官方网站外,还可以通过其他资源库如Apache Maven仓库(https://mvnrepository.com/)或JCenter(https://bintray.com/bintray/jcenter)下载Spring MVC、Spring 3和Hibernate 4的JAR包。在这些资源库的网站上搜索所需的JAR包,然后下载适合你的版本。 无论你选择哪种方式,下载所需的JAR包后,你可以将它们添加到你的项目构建路径中,以便在开发过程中使用Spring MVC、Spring 3和Hibernate 4的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值