SpringMvc+Spring+hibernate配置文件配置(注解)

本文详细介绍了Spring MVC框架的配置过程,包括web.xml中的Servlet、过滤器和监听器配置,application.xml中的数据源、事务管理和缓存配置,以及springmvc.xml中的视图解析器、拦截器和文件上传配置。

第一步 web.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. id="springBlade" metadata-complete="true" version="3.0">
  6. <display-name>smartweb</display-name>
  7. <context-param>
  8. <param-name>log4jConfigLocation</param-name>
  9. <param-value>classpath:log4j.properties</param-value>
  10. </context-param>
  11. <context-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>
  14. classpath*:application.xml;
  15. </param-value>
  16. </context-param>
  17. <listener>
  18. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  19. </listener>
  20. <servlet>
  21. <servlet-name>springmvc</servlet-name>
  22. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  23. <init-param>
  24. <param-name>contextConfigLocation</param-name>
  25. <param-value>classpath:springmvc.xml</param-value>
  26. </init-param>
  27. <load-on-startup>1</load-on-startup>
  28. <async-supported>true</async-supported>
  29. </servlet>
  30. <servlet-mapping>
  31. <servlet-name>springmvc</servlet-name>
  32. <url-pattern>/</url-pattern>
  33. </servlet-mapping>
  34. <servlet-mapping>
  35. <servlet-name>default</servlet-name>
  36. <url-pattern>/resource/*</url-pattern>
  37. </servlet-mapping>
  38. <servlet-mapping>
  39. <servlet-name>default</servlet-name>
  40. <url-pattern>*.html</url-pattern>
  41. </servlet-mapping>
  42. <filter>
  43. <filter-name>HiddenHttpMethodFilter</filter-name>
  44. <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  45. </filter>
  46. <filter>
  47. <filter-name>encodingFilter</filter-name>
  48. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  49. <init-param>
  50. <param-name>encoding</param-name>
  51. <param-value>UTF-8</param-value>
  52. </init-param>
  53. <init-param>
  54. <param-name>forceEncoding</param-name>
  55. <param-value>true</param-value>
  56. </init-param>
  57. </filter>
  58. <filter-mapping>
  59. <filter-name>encodingFilter</filter-name>
  60. <url-pattern>/*</url-pattern>
  61. </filter-mapping>
  62. <filter-mapping>
  63. <filter-name>HiddenHttpMethodFilter</filter-name>
  64. <servlet-name>springmvc</servlet-name>
  65. </filter-mapping>
  66. <filter>
  67. <filter-name>SpringOpenSessionInViewFilter</filter-name>
  68. <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  69. </filter>
  70. <filter-mapping>
  71. <filter-name>SpringOpenSessionInViewFilter</filter-name>
  72. <url-pattern>/*</url-pattern>
  73. </filter-mapping>
  74. <session-config>
  75. <session-timeout>120</session-timeout>
  76. </session-config>
  77. <welcome-file-list>
  78. <welcome-file>index.jsp</welcome-file>
  79. <welcome-file>index.html</welcome-file>
  80. </welcome-file-list>
  81. </web-app>

第二步  application.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:jaxws="http://cxf.apache.org/jaxws"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xmlns:cache="http://www.springframework.org/schema/cache"
  9. xsi:schemaLocation="http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-4.2.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
  15. http://www.springframework.org/schema/tx
  16. http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
  17. http://cxf.apache.org/jaxws
  18. http://cxf.apache.org/schemas/jaxws.xsd
  19. http://www.springframework.org/schema/cache
  20. http://www.springframework.org/schema/cache/spring-cache-4.3.xsd ">
  21. <context:property-placeholder location="classpath:jdbc.properties" />
  22. <context:component-scan base-package="org/eshop" >
  23. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
  24. </context:component-scan>
  25. <!-- base dataSource -->
  26. <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"
  27. destroy-method="close">
  28. <property name="driverClassName" value="${jdbc.driver}" />
  29. <property name="jdbcUrl" value="${jdbc.url}" />
  30. <property name="username" value="${jdbc.username}" />
  31. <property name="password" value="${jdbc.password}" />
  32. <property name="maximumPoolSize" value="100" />
  33. <property name="minimumIdle" value="${druid.minIdle}" />
  34. <property name="connectionTestQuery" value="${jdbc.validationQuery}" />
  35. <property name="dataSourceProperties">
  36. <props>
  37. <prop key="cachePrepStmts">true</prop>
  38. <prop key="prepStmtCacheSize">250</prop>
  39. <prop key="prepStmtCacheSqlLimit">2048</prop>
  40. <prop key="useServerPrepStmts">true</prop>
  41. </props>
  42. </property>
  43. </bean>
  44. <bean id="sessionFactory"
  45. class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  46. <property name="dataSource" ref="dataSource" />
  47. <property name="entityInterceptor" ref="entityInterceptor" />
  48. <property name="packagesToScan">
  49. <list>
  50. <value>org.eshop.*.*.entity</value>
  51. <value>org.eshop.*.entity</value>
  52. </list>
  53. </property>
  54. <property name="hibernateProperties">
  55. <props>
  56. <!-- 设置数据库方言 -->
  57. <prop key="hibernate.dialect">${hibernate.dialect}</prop>
  58. <!-- 输出SQL语句到控制台 -->
  59. <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
  60. <!-- 格式化输出到控制台的SQL语句 -->
  61. <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
  62. <!-- 是否开启二级缓存 -->
  63. <prop key="hibernate.cache.use_second_level_cache">false</prop>
  64. <!-- 配置二级缓存产品 -->
  65. <prop key="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</prop>
  66. <!-- 是否开启查询缓存 -->
  67. <prop key="hibernate.cache.use_query_cache">false</prop>
  68. <!-- 数据库批量查询数 -->
  69. <prop key="hibernate.jdbc.fetch_size">20</prop>
  70. <!-- 数据库批量更新数 -->
  71. <prop key="hibernate.jdbc.batch_size">20</prop>
  72. </props>
  73. </property>
  74. </bean>
  75. <bean id="transactionManager"
  76. class="org.springframework.orm.hibernate5.HibernateTransactionManager">
  77. <property name="sessionFactory" ref="sessionFactory" />
  78. </bean>
  79. <tx:annotation-driven transaction-manager="transactionManager" />
  80. <aop:config proxy-target-class="true">
  81. <aop:pointcut id="serviceMethod"
  82. expression=" execution(* org.eshop.*.*.service.*.*(..))" />
  83. <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
  84. </aop:config>
  85. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  86. <tx:attributes>
  87. <tx:method name="*" propagation="REQUIRED" />
  88. </tx:attributes>
  89. </tx:advice>
  90. <!-- 配置缓存 -->
  91. <cache:annotation-driven cache-manager="cacheManager" />
  92. <bean id="ehcacheManager"
  93. class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  94. <property name="configLocation" value="classpath:ehcache.xml" />
  95. </bean>
  96. <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
  97. <property name="cacheManager" ref="ehcacheManager" />
  98. <property name="transactionAware" value="true" />
  99. </bean>
  100. </beans>

 第三步 springmvc.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:context="http://www.springframework.org/schema/context"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:task="http://www.springframework.org/schema/task"
  7. xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context-4.2.xsd
  14. http://www.springframework.org/schema/mvc
  15. http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
  16. http://www.springframework.org/schema/task
  17. http://www.springframework.org/schema/task/spring-task-3.2.xsd
  18. http://www.directwebremoting.org/schema/spring-dwr
  19. http://directwebremoting.org/schema/spring-dwr-3.0.xsd
  20. ">
  21. <mvc:default-servlet-handler />
  22. <mvc:resources location="/resource/" mapping="/resource/**" />
  23. <aop:aspectj-autoproxy />
  24. <context:annotation-config />
  25. <context:component-scan base-package="org.eshop" use-default-filters="false">
  26. <context:include-filter type="annotation"
  27. expression="org.springframework.stereotype.Controller" />
  28. </context:component-scan>
  29. <mvc:annotation-driven />
  30. <mvc:interceptors>
  31. <mvc:interceptor>
  32. <!--匹配的是url路径, 如果不配置或/**,将拦截所有的Controller-->
  33. <mvc:mapping path="/**" />
  34. <mvc:exclude-mapping path="/ueditor/**"/>
  35. <bean class="org.eshop.core.web.interceptor.BaseInterceptor"></bean>
  36. </mvc:interceptor>
  37. <mvc:interceptor>
  38. <!--匹配的是url路径, 如果不配置或/**,将拦截所有的Controller-->
  39. <mvc:mapping path="/admin/**" />
  40. <mvc:exclude-mapping path="/admin/login"/>
  41. <mvc:exclude-mapping path="/admin/reg"/>
  42. <bean class="org.eshop.core.web.interceptor.AdminInterceptor"></bean>
  43. </mvc:interceptor>
  44. </mvc:interceptors>
  45. <!-- 当设置多个拦截器时,先按顺序调用preHandle方法,然后逆序调用每个拦截器的postHandle和afterCompletion方法 -->
  46. <bean id="messageSource"
  47. class="org.springframework.context.support.ResourceBundleMessageSource">
  48. <property name="basename">
  49. <value>i18n</value>
  50. </property>
  51. </bean>
  52. <bean id="localeResolver"
  53. class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
  54. <property name="cookieMaxAge" value="604800" />
  55. <property name="defaultLocale" value="zh_CN" />
  56. <property name="cookieName" value="Language"></property>
  57. </bean>
  58. <bean id="freemarkerConfig"
  59. class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
  60. <property name="templateLoaderPath" value="/WEB-INF/view/" />
  61. <property name="freemarkerSettings">
  62. <props>
  63. <prop key="template_update_delay">0</prop>
  64. <prop key="default_encoding">UTF-8</prop>
  65. <prop key="number_format">0.##########</prop>
  66. <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
  67. <prop key="classic_compatible">true</prop>
  68. <prop key="template_exception_handler">ignore</prop>
  69. <prop key="auto_import">spring.ftl as spring</prop>
  70. </props>
  71. </property>
  72. </bean>
  73. <bean id="beanNameViewResolver"
  74. class="org.springframework.web.servlet.view.BeanNameViewResolver">
  75. <property name="order" value="0" />
  76. </bean>
  77. <bean id="ftlViewResolver"
  78. class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
  79. <property name="viewClass" value="org.eshop.core.kit.MyFreeMarkerView" />
  80. <property name="order" value="1" />
  81. <property name="suffix" value=".ftl" />
  82. <property name="contentType" value="text/html;charset=UTF-8" />
  83. <property name="exposeRequestAttributes" value="true" />
  84. <property name="exposeSessionAttributes" value="true" />
  85. <property name="exposeSpringMacroHelpers" value="true" />
  86. <property name="allowRequestOverride" value="true" />
  87. <property name="requestContextAttribute" value="request" />
  88. <property name="cache" value="true" />
  89. </bean>
  90. <!-- 文件上传 -->
  91. <bean id="multipartResolver"
  92. class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  93. <!-- 默认编码 -->
  94. <property name="defaultEncoding" value="utf-8" />
  95. <!-- 设置上传文件总大小限制 10G -->
  96. <property name="maxUploadSize" value="10485760000"></property>
  97. <!-- 内存中的最大值 -->
  98. <property name="maxInMemorySize" value="40960" />
  99. </bean>
  100. </beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值