最近在学习SpringMVC,准备用SpringMVC做个项目,采用了SpringMVC+Spring+Hibernate。配置中包括了对事务的管理,拦截器配置,数据源和数据库连接池的配置,缓存的配置。贴在此处供以后查询使用。
web.xml的配置
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
- <display-name>bbs</display-name>
- <!--日志记录-->
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>/properties/log4j.properties</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
- <!--编码过滤-->
- <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>
- <servlet-name>dispathcer</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- /config/dispathcer-servlet.xml
- /config/applicationContext-*.xml
- </param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>dispathcer</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <!--错误页面的配置-->
- <error-page>
- <error-code>404</error-code>
- <location>/pages/error/404.jsp</location>
- </error-page>
- <error-page>
- <exception-type>java.lang.Exception</exception-type>
- <location>/pages/error/error.jsp</location>
- </error-page>
- <welcome-file-list>
- <welcome-file>/index</welcome-file>
- </welcome-file-list>
- </web-app>
dispacher-servlet.xml的配置
- <?xml version="1.0" encoding="UTF-8" ?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:cache="http://www.springframework.org/schema/cache"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.2.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
- http://www.springframework.org/schema/cache
- http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
- <!--扫描的包-->
- <context:component-scan base-package="com.bbs"/>
- <!--注解支持-->
- <mvc:annotation-driven/>
- <!--视图解析-->
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
- <property name="prefix" value="/pages/"/>
- <property name="suffix" value=".jsp"/>
- </bean>
- <!--静态文件的访问-->
- <mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>
- <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>
- <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>
- </beans>
applicationContext.xml的配置
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:cache="http://www.springframework.org/schema/cache"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.2.xsd
- http://www.springframework.org/schema/cache
- http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
- <!--propeties文件的配置-->
- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>/properties/bbs.properties</value>
- </list>
- </property>
- </bean>
- <!--sessionFactory的定义-->
- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
- <!--注入数据源-->
- <property name="dataSource" ref="dataSource"/>
- <property name="packagesToScan" value="com.bbs.model" />
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">${database.dialect}</prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">true</prop>
- <prop key="hibernate.hbm2ddl.auto">update</prop>
- </props>
- </property>
- </bean>
- <!--数据源的配置,使用c3p0-->
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
- <property name="driverClass" value="${database.driverClass}"/>
- <property name="jdbcUrl" value="${database.jdbcUrl}"/>
- <property name="user" value="${database.user}"/>
- <property name="password" value="${database.password}"/>
- <property name="maxPoolSize" value="80"/>
- <property name="minPoolSize" value="10"/>
- <property name="initialPoolSize" value="1"/>
- <property name="maxIdleTime" value="20"/>
- </bean>
- <!--事务管理的配置-->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate4.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <bean id="transactionInterceptor"
- class="org.springframework.transaction.interceptor.TransactionInterceptor">
- <property name="transactionManager" ref="transactionManager" />
- <!-- 配置事务属性 -->
- <property name="transactionAttributes">
- <props>
- <prop key="save*">PROPAGATION_REQUIRED</prop>
- <prop key="insert*">PROPAGATION_REQUIRED</prop>
- <prop key="create*">PROPAGATION_REQUIRED</prop>
- <prop key="update*">PROPAGATION_REQUIRED</prop>
- <prop key="delete*">PROPAGATION_REQUIRED</prop>
- <prop key="remove*">PROPAGATION_REQUIRED</prop>
- <prop key="batch*">PROPAGATION_REQUIRED</prop>
- <prop key="execute*">PROPAGATION_REQUIRED</prop>
- <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="search*">PROPAGATION_REQUIRED,readOnly</prop>
- </props>
- </property>
- </bean>
- <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="beanNames">
- <list>
- <!--所有名字以ServiceImpl结尾的bean自动配置事务-->
- <!--因为我们是以注解的方式实现的,默认bean的名字就是类的名称-->
- <!--平时我们以xml方式配置时,一般给以Service结尾-->
- <value>*ServiceImpl</value>
- </list>
- </property>
- <property name="interceptorNames">
- <list>
- <value>transactionInterceptor</value>
- </list>
- </property>
- </bean>
- <cache:annotation-driven/>
- <!--缓存的配置-->
- <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" >
- <property name="cacheManager">
- <ref local="cacheMg"/>
- </property>
- </bean>
- <bean id="cacheMg" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >
- <property name="configLocation">
- <value>/config/ehcache.xml</value>
- </property>
- </bean>
- <!--拦截器
- -->
- <mvc:interceptors >
- <mvc:interceptor>
- <mvc:mapping path="/*"/>
- <mvc:exclude-mapping path="/do"/>
- <bean class="com.bbs.interceptor.LoginInterceptor"/>
- </mvc:interceptor>
- </mvc:interceptors>
- </beans>
项目下载地址:http://download.csdn.net/detail/zh_w_h163/6557989