SpringMVC 配置过程及详解
加入jar包
在web.xml中
添加spring监听器
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
添加spring容器(父容器)配置文件:
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/config/application-context.xml <!--声明数据库连接参数和事务管理--> /WEB-INF/config/customer-admin-manage.xml <!--dao和service--> </param-value> </context-param>
spring配置文件application-context.xml
数据库配置文件 jdbc.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/cms?characterEncoding=UTF-8 jdbc.username=root jdbc.password=123456 ...
添加到spring父容器的配置文件application-context.xml中
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>/WEB-INF/config/jdbc.properties</value> </list> </property> </bean>
配置dataSource:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClassName}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="autoCommitOnClose" value="true"/> <property name="checkoutTimeout" value="${cpool.checkoutTimeout}"/> <property name="initialPoolSize" value="${cpool.minPoolSize}"/> <property name="minPoolSize" value="${cpool.minPoolSize}"/> <property name="maxPoolSize" value="${cpool.maxPoolSize}"/> <property name="maxIdleTime" value="${cpool.maxIdleTime}"/> <property name="acquireIncrement" value="${cpool.acquireIncrement}"/> <property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}"/> </bean>
配置sessionFactory:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingLocations"> <list> <value>classpath*:/com/cms/customer/entity/hbm/*.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect hibernate.show_sql=false hibernate.format_sql=false hibernate.query.substitutions=true 1, false 0 hibernate.jdbc.batch_size=20 hibernate.cache.use_query_cache=true </value> </property> <property name="entityInterceptor">< !-- 配置Hibernate拦截器,自动填充数据的插入、更新时间(不知道什么意思)--> <ref local="treeInterceptor"/> </property> <property name="cacheProvider">< !-- 为WEB应用提供缓存。 --> <ref local="cacheProvider"/> </property> <property name="lobHandler">< !-- spring提供的操作lob字段。<bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true"/> --> <ref bean="lobHandler" /> </property> </bean>
基于全注解的事务声明管理
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <context:annotation-config/> <tx:annotation-driven transaction-manager="transactionManager" />
对于context:annotation-config
他的作用是隐式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor(想使用Autowired注解)、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor这 4 个BeanPostProcessor。 另外,<context:component-scan base-package=”XX.XX”/>(用于自动扫描需要注入的bean) 包含了<context:annotation-config/>的功能,在这里没有使用<context:component-scan base-package=”XX.XX”/>,则必须将bean添加到父容器的(dao,service,manager之类的)xml中
对于tx:annotation-driven ,则表示所有Transactional注解了的manager都使用这个事务管理
配置springmvc(子容器)
添加springmvc的Servlet
<servlet> <servlet-name>admin</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name><!--指定注入action的配置文件,如果没有指定,则默认在web-inf下查找admin-servlet.xml --> <param-value>/WEB-INF/config/admin.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
每一个servlet对应一个配置文件,用于映射不同的请求路径集合
<servlet> <servlet-name>front</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/front.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet>
对于admin.xml
<value>/WEB-INF/languages/core_admin/messages</value> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="cacheSeconds" value="-1"/> <property name="basenames"> <list> <value>/WEB-INF/languages/core_admin/messages</value> </list> </property> </bean> <!--文件上传--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/> <!--通过注解,把一个URL映射到Controller类的方法上--> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="webBindingInitializer"><!--重写WebBindingInitializer--> <bean class=" com.cms.common.web.springmvc.BindingInitializer"/> </property> </bean> <!--用于Spring 从外部属性文件中载入属性,并使用这些属性值替换Spring 配置文件中的占位符变量(${varible})。 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>/WEB-INF/config/firewall.properties</value> </list> </property> </bean> <!-- DefaultAnnotationHandlerMapping-映射url到被RequestMapping注解的controller或者下面的方法--> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> <ref bean="adminContextInterceptor"/> <ref bean="adminLocaleIntercept"/> <ref bean="fireWallInterceptor"/> </list> </property> </bean> <!--拦截器--> <bean id="adminContextInterceptor" class="com.cms.cms.web.AdminContextInterceptor"> <property name="auth" value="true"/> <property name="loginUrl" value="/admin/cms/login.do"/> <property name="returnUrl" value="/admin/cms/index.do"/> <property name="excludeUrls"> <list> <value>/login.do</value> <value>/logout.do</value> </list> </property> </bean> <bean id="adminLocaleIntercept" class="com.cms.cms.web.AdminLocaleInterceptor"/> <bean id="fireWallInterceptor" class="com.cms.cms.web.FireWallInterceptor"></bean> <!--Cookie相关--> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> <property name="cookieName" value="clientlanguage"/> <property name="cookieMaxAge" value="-1"/> </bean> <!--定义一场处理--> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="org.springframework.web.bind.MissingServletRequestParameterException">/error/requiredParameter</prop> <prop key="org.springframework.beans.TypeMismatchException">/error/mismatchParameter</prop> <prop key="org.springframework.web.bind.ServletRequestBindingException">/error/bindException</prop> <prop key="org.springframework.dao.DataIntegrityViolationException">/error/integrityViolation</prop> </props> </property> </bean> <!--freemarker配置--> <bean id="freemarkerViewResolver" class="com.cms.common.web.springmvc.RichFreeMarkerViewResolver"> <property name="prefix" value="/cms_sys/"/> <property name="suffix" value=".html"/> <property name="contentType" value="text/html; charset=UTF-8"/> <property name="exposeRequestAttributes" value="false"/> <property name="exposeSessionAttributes" value="false"/> <property name="exposeSpringMacroHelpers" value="true"/> </bean> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF"/> <property name="freemarkerVariables"> <map> <!--在FCK编辑器中需要用到appBase,以确定connector路径。--> <entry key="appBase" value="/admin/cms"/> <!--后台管理权限控制--> <entry key="cms_perm" value-ref="cms_perm"/> <entry key="text_cut" value-ref="text_cut"/> <entry key="html_cut" value-ref="html_cut"/> </map> </property> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">0</prop> <prop key="defaultEncoding">UTF-8</prop> <prop key="url_escaping_charset">UTF-8</prop> <prop key="locale">zh_CN</prop> <prop key="boolean_format">true,false</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop key="date_format">yyyy-MM-dd</prop> <prop key="time_format">HH:mm:ss</prop> <prop key="number_format">0.######</prop> <prop key="whitespace_stripping">true</prop> <prop key="auto_import">/ftl/cms/index.ftl as p,/ftl/spring.ftl as s</prop> </props> </property> </bean> <!--见89行--> <context:annotation-config/> <!--action注入配置文件--> <import resource="admin-action.xml"/>
对于:admin-action.xml
<bean id="customerAct" class="com.customer.action.CustomerAct"/> <bean id="basedataAct" class="com.customer.action.BasedataAct"/> <bean id="employeeAct" class="com.customer.action.EmployeeAct"/> <bean id="projectAct" class="com.customer.action.ProjectAct"/> <bean id="productAct" class="com.customer.action.ProductAct"/>
每一个action都要用Controller注解,其中使用的manager属性用Autowired注解,每一个方法都要用RequestMapping注解
每一个manager都要用Service,Transactional注解
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!