springMVC+Hibernate的配置

最近在学习SpringMVC,准备用SpringMVC做个项目,采用了SpringMVC+Spring+Hibernate。配置中包括了对事务的管理,拦截器配置,数据源和数据库连接池的配置,缓存的配置。贴在此处供以后查询使用。

web.xml的配置

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.     <display-name>bbs</display-name>  
  4.     <!--日志记录-->  
  5.     <context-param>    
  6.        <param-name>log4jConfigLocation</param-name>    
  7.         <param-value>/properties/log4j.properties</param-value>    
  8.     </context-param>    
  9.     <listener>    
  10.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>    
  11.     </listener>  
  12.     <!--编码过滤-->  
  13.     <filter>    
  14.         <filter-name>CharacterEncodingFilter</filter-name>    
  15.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    
  16.         <init-param>    
  17.             <param-name>encoding</param-name>    
  18.             <param-value>UTF-8</param-value>    
  19.         </init-param>    
  20.         <init-param>    
  21.             <param-name>forceEncoding</param-name>    
  22.             <param-value>true</param-value><!-- 强制进行转码 -->    
  23.         </init-param>    
  24.     </filter>   
  25.     <filter-mapping>  
  26.         <filter-name>CharacterEncodingFilter</filter-name>  
  27.         <url-pattern>/*</url-pattern>  
  28.     </filter-mapping>  
  29.     <servlet>  
  30.         <servlet-name>dispathcer</servlet-name>  
  31.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  32.         <init-param>  
  33.             <param-name>contextConfigLocation</param-name>    
  34.             <param-value>  
  35.                 /config/dispathcer-servlet.xml  
  36.                 /config/applicationContext-*.xml  
  37.             </param-value>  
  38.         </init-param>  
  39.         <load-on-startup>1</load-on-startup>  
  40.     </servlet>  
  41.     <servlet-mapping>  
  42.         <servlet-name>dispathcer</servlet-name>  
  43.         <url-pattern>/</url-pattern>  
  44.     </servlet-mapping>  
  45.     <!--错误页面的配置-->  
  46.     <error-page>  
  47.        <error-code>404</error-code>  
  48.        <location>/pages/error/404.jsp</location>  
  49.     </error-page>  
  50.     <error-page>  
  51.       <exception-type>java.lang.Exception</exception-type>  
  52.       <location>/pages/error/error.jsp</location>  
  53.     </error-page>  
  54.     <welcome-file-list>  
  55.         <welcome-file>/index</welcome-file>  
  56.     </welcome-file-list>  
  57. </web-app>  

dispacher-servlet.xml的配置

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  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:context="http://www.springframework.org/schema/context"    
  5.         xmlns:mvc="http://www.springframework.org/schema/mvc"    
  6.         xmlns:cache="http://www.springframework.org/schema/cache"  
  7.         xsi:schemaLocation="    
  8.           http://www.springframework.org/schema/beans    
  9.           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd    
  10.           http://www.springframework.org/schema/context    
  11.           http://www.springframework.org/schema/context/spring-context-3.2.xsd    
  12.           http://www.springframework.org/schema/mvc        
  13.           http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  
  14.           http://www.springframework.org/schema/cache   
  15.           http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">  
  16.   <!--扫描的包-->  
  17.   <context:component-scan base-package="com.bbs"/>  
  18.   <!--注解支持-->  
  19.   <mvc:annotation-driven/>  
  20.   <!--视图解析-->  
  21.   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">   
  22.         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>    
  23.         <property name="prefix" value="/pages/"/>    
  24.         <property name="suffix" value=".jsp"/>   
  25.   </bean>   
  26.   <!--静态文件的访问-->  
  27.   <mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>    
  28.   <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>    
  29.   <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>   
  30.     
  31. </beans>  

applicationContext.xml的配置

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  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:context="http://www.springframework.org/schema/context"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xmlns:cache="http://www.springframework.org/schema/cache"  
  7.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  8.     xsi:schemaLocation="  
  9.         http://www.springframework.org/schema/beans  
  10.         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  11.         http://www.springframework.org/schema/context  
  12.         http://www.springframework.org/schema/context/spring-context-3.2.xsd  
  13.         http://www.springframework.org/schema/cache   
  14.         http://www.springframework.org/schema/cache/spring-cache-3.2.xsd  
  15.         http://www.springframework.org/schema/mvc   
  16.         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  
  17.     <!--propeties文件的配置-->  
  18.     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  19.         <property name="locations">  
  20.              <list>  
  21.                 <value>/properties/bbs.properties</value>  
  22.             </list>  
  23.         </property>  
  24.     </bean>  
  25.     <!--sessionFactory的定义-->  
  26.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  27.         <!--注入数据源-->  
  28.         <property name="dataSource" ref="dataSource"/>  
  29.         <property name="packagesToScan" value="com.bbs.model" />  
  30.         <property name="hibernateProperties">  
  31.             <props>  
  32.                 <prop key="hibernate.dialect">${database.dialect}</prop>  
  33.                 <prop key="hibernate.show_sql">true</prop>  
  34.                 <prop key="hibernate.format_sql">true</prop>  
  35.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  36.             </props>  
  37.         </property>  
  38.     </bean>  
  39.     <!--数据源的配置,使用c3p0-->  
  40.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">  
  41.         <property name="driverClass" value="${database.driverClass}"/>  
  42.         <property name="jdbcUrl" value="${database.jdbcUrl}"/>  
  43.         <property name="user" value="${database.user}"/>  
  44.         <property name="password" value="${database.password}"/>  
  45.         <property name="maxPoolSize" value="80"/>  
  46.         <property name="minPoolSize" value="10"/>  
  47.         <property name="initialPoolSize" value="1"/>  
  48.         <property name="maxIdleTime" value="20"/>  
  49.     </bean>  
  50.     <!--事务管理的配置-->  
  51.     <bean id="transactionManager"  
  52.         class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
  53.         <property name="sessionFactory" ref="sessionFactory" />  
  54.     </bean>   
  55.      <bean id="transactionInterceptor"   
  56.         class="org.springframework.transaction.interceptor.TransactionInterceptor">   
  57.         <property name="transactionManager" ref="transactionManager" />   
  58.         <!-- 配置事务属性 -->   
  59.         <property name="transactionAttributes">   
  60.             <props>   
  61.                 <prop key="save*">PROPAGATION_REQUIRED</prop>  
  62.                 <prop key="insert*">PROPAGATION_REQUIRED</prop>  
  63.                 <prop key="create*">PROPAGATION_REQUIRED</prop>  
  64.                 <prop key="update*">PROPAGATION_REQUIRED</prop>  
  65.                 <prop key="delete*">PROPAGATION_REQUIRED</prop>  
  66.                 <prop key="remove*">PROPAGATION_REQUIRED</prop>           
  67.                 <prop key="batch*">PROPAGATION_REQUIRED</prop>  
  68.                 <prop key="execute*">PROPAGATION_REQUIRED</prop>  
  69.                 <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>  
  70.                 <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>  
  71.                 <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>  
  72.                 <prop key="search*">PROPAGATION_REQUIRED,readOnly</prop>   
  73.             </props>   
  74.         </property>   
  75.     </bean>  
  76.     <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  77.         <property name="beanNames">   
  78.             <list>   
  79.                 <!--所有名字以ServiceImpl结尾的bean自动配置事务-->  
  80.                 <!--因为我们是以注解的方式实现的,默认bean的名字就是类的名称-->  
  81.                 <!--平时我们以xml方式配置时,一般给以Service结尾-->  
  82.                 <value>*ServiceImpl</value>  
  83.             </list>   
  84.         </property>   
  85.         <property name="interceptorNames">   
  86.             <list>   
  87.                 <value>transactionInterceptor</value>   
  88.             </list>   
  89.         </property>   
  90.     </bean>  
  91.     <cache:annotation-driven/>  
  92.     <!--缓存的配置-->   
  93.     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" >  
  94.         <property name="cacheManager">  
  95.             <ref local="cacheMg"/>  
  96.         </property>  
  97.     </bean>  
  98.     <bean id="cacheMg" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >  
  99.         <property name="configLocation">  
  100.             <value>/config/ehcache.xml</value>  
  101.         </property>  
  102.     </bean>  
  103.     <!--拦截器 
  104.     -->  
  105.     <mvc:interceptors >  
  106.         <mvc:interceptor>  
  107.             <mvc:mapping path="/*"/>  
  108.                         <mvc:exclude-mapping path="/do"/>  
  109.             <bean class="com.bbs.interceptor.LoginInterceptor"/>  
  110.         </mvc:interceptor>  
  111.     </mvc:interceptors>   
  112. </beans>  


项目下载地址:http://download.csdn.net/detail/zh_w_h163/6557989
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值