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

3 篇文章 0 订阅
2 篇文章 0 订阅

第一步 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" xmlns:web="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_3_0.xsd"
	id="springBlade" metadata-complete="true" version="3.0">
	<display-name>smartweb</display-name>
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:log4j.properties</param-value>
	</context-param>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath*:application.xml;
		</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<async-supported>true</async-supported>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>/resource/*</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>

	<filter>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
	</filter>
	<filter>
		<filter-name>encodingFilter</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>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<servlet-name>springmvc</servlet-name>
	</filter-mapping>
	<filter>
		<filter-name>SpringOpenSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>SpringOpenSessionInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<session-config>
		<session-timeout>120</session-timeout>
	</session-config>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
</web-app>

第二步  application.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:jaxws="http://cxf.apache.org/jaxws" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:cache="http://www.springframework.org/schema/cache" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://cxf.apache.org/jaxws     
    http://cxf.apache.org/schemas/jaxws.xsd 
    http://www.springframework.org/schema/cache 
    	http://www.springframework.org/schema/cache/spring-cache-4.3.xsd ">
	<context:property-placeholder location="classpath:jdbc.properties" />
	<context:component-scan base-package="org/eshop" >
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

	<!-- base dataSource -->
	<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maximumPoolSize" value="100" />
		<property name="minimumIdle" value="${druid.minIdle}" />
		<property name="connectionTestQuery" value="${jdbc.validationQuery}" />
		<property name="dataSourceProperties">
			<props>
				<prop key="cachePrepStmts">true</prop>
				<prop key="prepStmtCacheSize">250</prop>
				<prop key="prepStmtCacheSqlLimit">2048</prop>
				<prop key="useServerPrepStmts">true</prop>
			</props>
		</property>
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="entityInterceptor" ref="entityInterceptor" />
		<property name="packagesToScan">
			<list>
				<value>org.eshop.*.*.entity</value>
				<value>org.eshop.*.entity</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<!-- 设置数据库方言 -->
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<!-- 输出SQL语句到控制台 -->
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<!-- 格式化输出到控制台的SQL语句 -->
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
				<!-- 是否开启二级缓存 -->
				<prop key="hibernate.cache.use_second_level_cache">false</prop>
				<!-- 配置二级缓存产品 -->
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</prop>
				<!-- 是否开启查询缓存 -->
				<prop key="hibernate.cache.use_query_cache">false</prop>
				<!-- 数据库批量查询数 -->
				<prop key="hibernate.jdbc.fetch_size">20</prop>
				<!-- 数据库批量更新数 -->
				<prop key="hibernate.jdbc.batch_size">20</prop>
			</props>
		</property>
	</bean>
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<tx:annotation-driven transaction-manager="transactionManager" />
	<aop:config proxy-target-class="true">
		<aop:pointcut id="serviceMethod"
			expression=" execution(* org.eshop.*.*.service.*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
	</aop:config>
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>
	
	<!-- 配置缓存 -->
	<cache:annotation-driven cache-manager="cacheManager" />

	<bean id="ehcacheManager"
		class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
		<property name="configLocation" value="classpath:ehcache.xml" />
	</bean>
	
	<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
		<property name="cacheManager" ref="ehcacheManager" />
		<property name="transactionAware" value="true" />
	</bean>

</beans>

 第三步 springmvc.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:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:task="http://www.springframework.org/schema/task" 
	xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"  
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    http://www.springframework.org/schema/task  
	http://www.springframework.org/schema/task/spring-task-3.2.xsd 
	http://www.directwebremoting.org/schema/spring-dwr  
	http://directwebremoting.org/schema/spring-dwr-3.0.xsd 
   	">
    
	<mvc:default-servlet-handler />
	<mvc:resources location="/resource/" mapping="/resource/**" />
	<aop:aspectj-autoproxy />
	<context:annotation-config />
      <context:component-scan base-package="org.eshop" use-default-filters="false">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
	<mvc:annotation-driven />
	<mvc:interceptors>
		<mvc:interceptor>
			<!--匹配的是url路径, 如果不配置或/**,将拦截所有的Controller-->
			<mvc:mapping path="/**" />
			<mvc:exclude-mapping path="/ueditor/**"/>
			<bean class="org.eshop.core.web.interceptor.BaseInterceptor"></bean>
		</mvc:interceptor> 
	    <mvc:interceptor>
			<!--匹配的是url路径, 如果不配置或/**,将拦截所有的Controller-->
			<mvc:mapping path="/admin/**" />
			<mvc:exclude-mapping path="/admin/login"/>
			<mvc:exclude-mapping path="/admin/reg"/>
			<bean class="org.eshop.core.web.interceptor.AdminInterceptor"></bean>
		</mvc:interceptor> 
	</mvc:interceptors>
	<!-- 当设置多个拦截器时,先按顺序调用preHandle方法,然后逆序调用每个拦截器的postHandle和afterCompletion方法 -->
	
	<bean id="messageSource"
		class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basename">
			<value>i18n</value>
		</property>
	</bean>
	<bean id="localeResolver"
		class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
		<property name="cookieMaxAge" value="604800" />
		<property name="defaultLocale" value="zh_CN" />
		<property name="cookieName" value="Language"></property>
	</bean>
	<bean id="freemarkerConfig"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="/WEB-INF/view/" />
		<property name="freemarkerSettings">
			<props>
				<prop key="template_update_delay">0</prop>
				<prop key="default_encoding">UTF-8</prop>
				<prop key="number_format">0.##########</prop>
				<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
				<prop key="classic_compatible">true</prop>
				<prop key="template_exception_handler">ignore</prop>
				<prop key="auto_import">spring.ftl as spring</prop>
			</props>
		</property>
	</bean>
	<bean id="beanNameViewResolver"
		class="org.springframework.web.servlet.view.BeanNameViewResolver">
		<property name="order" value="0" />
	</bean>
	<bean id="ftlViewResolver"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<property name="viewClass" value="org.eshop.core.kit.MyFreeMarkerView" />
		<property name="order" value="1" />
		<property name="suffix" value=".ftl" />
		<property name="contentType" value="text/html;charset=UTF-8" />
		<property name="exposeRequestAttributes" value="true" />
		<property name="exposeSessionAttributes" value="true" />
		<property name="exposeSpringMacroHelpers" value="true" />
		<property name="allowRequestOverride" value="true" />
		<property name="requestContextAttribute" value="request" />
		<property name="cache" value="true" />
	</bean>

	<!-- 文件上传 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 默认编码 -->
		<property name="defaultEncoding" value="utf-8" />
		<!-- 设置上传文件总大小限制 10G -->
		<property name="maxUploadSize" value="10485760000"></property>
		<!-- 内存中的最大值 -->
		<property name="maxInMemorySize" value="40960" />
	</bean>
	
	
</beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值