SpringMVC 配置过程及详解

来源:互联网 发布:小猪cms是做什么的 编辑:程序博客网 时间:2024/05/22 07:58

加入jar包

在web.xml中

添加spring监听器

1<listener>
2    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
3</listener>

添加spring容器(父容器)配置文件:

1<context-param>
2    <param-name>contextConfigLocation</param-name>
3    <param-value>
4        /WEB-INF/config/application-context.xml     <!--声明数据库连接参数和事务管理-->
5        /WEB-INF/config/customer-admin-manage.xml   <!--dao和service-->
6    </param-value>
7</context-param>

spring配置文件application-context.xml

数据库配置文件 jdbc.properties:

1jdbc.driverClassName=com.mysql.jdbc.Driver
2jdbc.url=jdbc:mysql://localhost:3306/cms?characterEncoding=UTF-8
3jdbc.username=root
4jdbc.password=123456
5...

添加到spring父容器的配置文件application-context.xml中

1<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
2    <property name="locations">
3        <list>
4            <value>/WEB-INF/config/jdbc.properties</value>
5        </list>
6    </property>
7</bean>

配置dataSource:

01<bean id="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource">
02    <property name="driverClass"value="${jdbc.driverClassName}"/>
03    <property name="jdbcUrl"value="${jdbc.url}" />
04    <property name="user"value="${jdbc.username}" />
05    <property name="password"value="${jdbc.password}" />
06    <property name="autoCommitOnClose"value="true"/>
07    <property name="checkoutTimeout"value="${cpool.checkoutTimeout}"/>
08    <property name="initialPoolSize"value="${cpool.minPoolSize}"/>
09    <property name="minPoolSize"value="${cpool.minPoolSize}"/>
10    <property name="maxPoolSize"value="${cpool.maxPoolSize}"/>
11    <property name="maxIdleTime"value="${cpool.maxIdleTime}"/>
12    <property name="acquireIncrement"value="${cpool.acquireIncrement}"/>
13    <property name="maxIdleTimeExcessConnections"value="${cpool.maxIdleTimeExcessConnections}"/>
14</bean>

配置sessionFactory:

01<bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
02    <property name="dataSource"ref="dataSource"/> 
03    <property name="mappingLocations">
04        <list>
05            <value>classpath*:/com/cms/customer/entity/hbm/*.hbm.xml</value>
06        </list>
07    </property>
08    <property name="hibernateProperties">
09        <value>
10        hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
11        hibernate.show_sql=false
12        hibernate.format_sql=false
13        hibernate.query.substitutions=true1, false0
14        hibernate.jdbc.batch_size=20
15        hibernate.cache.use_query_cache=true
16        </value>
17    </property>
18    <property name="entityInterceptor">< !-- 配置Hibernate拦截器,自动填充数据的插入、更新时间(不知道什么意思)-->
19        <ref local="treeInterceptor"/>
20    </property>
21    <property name="cacheProvider">< !-- 为WEB应用提供缓存。 -->
22        <ref local="cacheProvider"/>
23    </property>
24    <property name="lobHandler">< !-- spring提供的操作lob字段。<bean id="lobHandler"class="org.springframework.jdbc.support.lob.DefaultLobHandler"lazy-init="true"/> -->
25        <ref bean="lobHandler"/>
26    </property>
27</bean>

基于全注解的事务声明管理

1<bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
2    <property name="sessionFactory"ref="sessionFactory" />
3</bean>
4<context:annotation-config/>
5<tx:annotation-driven transaction-manager="transactionManager"/>

对于context:annotation-config

1他的作用是隐式地向 Spring 容器注册  
2AutowiredAnnotationBeanPostProcessor(想使用Autowired注解)、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor这4 个BeanPostProcessor。
3另外,<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

1<servlet>
2    <servlet-name>admin</servlet-name>
3    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
4    <init-param>
5        <param-name>contextConfigLocation</param-name><!--指定注入action的配置文件,如果没有指定,则默认在web-inf下查找admin-servlet.xml    -->
6        <param-value>/WEB-INF/config/admin.xml</param-value>
7    </init-param>
8    <load-on-startup>1</load-on-startup>
9</servlet>

每一个servlet对应一个配置文件,用于映射不同的请求路径集合

1<servlet>
2    <servlet-name>front</servlet-name>
3    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
4    <init-param>
5        <param-name>contextConfigLocation</param-name>
6        <param-value>/WEB-INF/config/front.xml</param-value>
7    </init-param>
8    <load-on-startup>2</load-on-startup>
9</servlet>

对于admin.xml

001<value>/WEB-INF/languages/core_admin/messages</value>
002<bean id="messageSource"class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
003    <property name="cacheSeconds"value="-1"/>
004    <property name="basenames">
005        <list>
006            <value>/WEB-INF/languages/core_admin/messages</value>
007        </list>
008    </property>
009</bean>
010 
011<!--文件上传-->
012<bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
013 
014<!--通过注解,把一个URL映射到Controller类的方法上-->
015<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
016    <property name="webBindingInitializer"><!--重写WebBindingInitializer-->
017        <beanclass=" com.cms.common.web.springmvc.BindingInitializer"/>
018    </property>
019</bean>
020 
021<!--用于Spring 从外部属性文件中载入属性,并使用这些属性值替换Spring 配置文件中的占位符变量(${varible})。 -->
022<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
023    <property name="locations">
024        <list>
025            <value>/WEB-INF/config/firewall.properties</value>
026        </list>
027    </property>
028</bean>
029 
030<!--    DefaultAnnotationHandlerMapping-映射url到被RequestMapping注解的controller或者下面的方法-->
031<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
032    <property name="interceptors">
033        <list>
034            <ref bean="adminContextInterceptor"/>
035            <ref bean="adminLocaleIntercept"/>
036            <ref bean="fireWallInterceptor"/>
037        </list>
038    </property>
039</bean>
040<!--拦截器-->
041<bean id="adminContextInterceptor"class="com.cms.cms.web.AdminContextInterceptor">
042    <property name="auth"value="true"/>
043    <property name="loginUrl"value="/admin/cms/login.do"/>
044    <property name="returnUrl"value="/admin/cms/index.do"/>
045    <property name="excludeUrls">
046        <list>
047            <value>/login.do</value>
048            <value>/logout.do</value>
049        </list>
050    </property>
051</bean>
052<bean id="adminLocaleIntercept"class="com.cms.cms.web.AdminLocaleInterceptor"/>
053<bean id="fireWallInterceptor"class="com.cms.cms.web.FireWallInterceptor"></bean>
054 
055<!--Cookie相关-->
056<bean id="localeResolver"class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
057    <property name="cookieName"value="clientlanguage"/>
058    <property name="cookieMaxAge"value="-1"/>
059</bean>
060 
061<!--定义一场处理-->
062<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
063    <property name="exceptionMappings">
064        <props>
065            <prop key="org.springframework.web.bind.MissingServletRequestParameterException">/error/requiredParameter</prop>
066            <prop key="org.springframework.beans.TypeMismatchException">/error/mismatchParameter</prop>
067            <prop key="org.springframework.web.bind.ServletRequestBindingException">/error/bindException</prop>
068            <prop key="org.springframework.dao.DataIntegrityViolationException">/error/integrityViolation</prop>
069        </props>
070    </property>
071</bean>
072<!--freemarker配置-->
073<bean id="freemarkerViewResolver"class="com.cms.common.web.springmvc.RichFreeMarkerViewResolver">
074    <property name="prefix"value="/cms_sys/"/>
075    <property name="suffix"value=".html"/>
076    <property name="contentType"value="text/html; charset=UTF-8"/>
077    <property name="exposeRequestAttributes"value="false"/>
078    <property name="exposeSessionAttributes"value="false"/>
079    <property name="exposeSpringMacroHelpers"value="true"/>
080</bean>
081<bean id="freemarkerConfig"class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
082    <property name="templateLoaderPath"value="/WEB-INF"/>
083    <property name="freemarkerVariables">
084        <map>
085            <!--在FCK编辑器中需要用到appBase,以确定connector路径。-->
086            <entry key="appBase"value="/admin/cms"/>
087            <!--后台管理权限控制-->
088            <entry key="cms_perm"value-ref="cms_perm"/>
089            <entry key="text_cut"value-ref="text_cut"/>
090            <entry key="html_cut"value-ref="html_cut"/>
091        </map>
092    </property>
093    <property name="freemarkerSettings">
094        <props>
095            <prop key="template_update_delay">0</prop>
096            <prop key="defaultEncoding">UTF-8</prop>
097            <prop key="url_escaping_charset">UTF-8</prop>
098            <prop key="locale">zh_CN</prop>
099            <prop key="boolean_format">true,false</prop>
100            <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
101            <prop key="date_format">yyyy-MM-dd</prop>
102            <prop key="time_format">HH:mm:ss</prop>
103            <prop key="number_format">0.######</prop>
104            <prop key="whitespace_stripping">true</prop>
105            <prop key="auto_import">/ftl/cms/index.ftl as p,/ftl/spring.ftl as s</prop>
106        </props>
107    </property>
108</bean>
109 
110<!--见89行-->
111<context:annotation-config/>
112 
113<!--action注入配置文件-->
114<importresource="admin-action.xml"/>

对于:admin-action.xml

1<bean id="customerAct"class="com.customer.action.CustomerAct"/>
2<bean id="basedataAct"class="com.customer.action.BasedataAct"/>
3<bean id="employeeAct"class="com.customer.action.EmployeeAct"/>
4<bean id="projectAct"class="com.customer.action.ProjectAct"/>
5<bean id="productAct"class="com.customer.action.ProductAct"/>

每一个action都要用Controller注解,其中使用的manager属性用Autowired注解,每一个方法都要用RequestMapping注解
每一个manager都要用Service,Transactional注解


0 0