web.xml
- <welcome-file-list>[欢迎页面,可定义多个,会依次查找可用视图]
- <listener>
- <listener-class>基本配置包含Log4jConfigListener和ContextLoaderListener,且log4j监听器在前,目前已废除log4j监听器,原因还在努力追问
- <context-param>指定上下文配置文件路径,基本配置包含log4j和Spring配置文件
- <param-name>指定上下文名称,一般为:名称+ConfigLocation后缀,如:contextConfigLocation,不可随意定义,否则指定的配置文件无法加载成功,实际上它是org.springframework.web.servlet.FrameworkServlet中的一个成员变量,而FrameworkServlet是DispatcherServlet的父类,log4jConfigLocation目前不得而知
- <param-value>指定上下文路径,如:classpath:applicationContext.xml
- <servlet>
- <servlet-name>Servlet名称,可以自定义,但是需要遵守规则:比如指定为Spring,那么最好在classpath路径中配置Spring-servlet.xml,否则需要在子元素<init-param>特别指出
- <servlet-class>因为要配置MVC,所以指定为:org.springframework.web.servlet.DispatcherServlet
- <init-param>[定义容器启动时初始化的配置文件,作用主要是指定自定义配置文件的路径,貌似可以指定多个]
- <param-name>[contextConfigLocation,不可更改,原因见3.1]
- <param-value>[可以自定义,如:classpath:spring-servlet.xml,如果不定义,那么默认为:classpath:${servlet-name}-servlet.xml,见4.1]
- <load-on-startup>[定义为1,表示启动等级,参考文章]
- <servlet-mapping>
- <servlet-name>与4.1保持一致
- <url-pattern>一般定义为“/”,表示所有请求都通过DispatcherServlet来处理
- <filter>[以字符集为例]
- <filter-name>[自行指定]
- <filter-class>[org.springframework.web.filter.CharacterEncodingFilter]
- <init-param>
- <param-name>[encoding,不可更改,它是CharacterEncodingFilter中定义的一个成员变量]
- <param-value>[UTF-8]
- <filter-mapping>
- <filter-name>[与6.1保持一致]
- <url-pattern>[/*表示所有请求都经过此过滤器过滤]
WEB.XML
<!-- Spring MVC配置 --> <!-- ====================================== --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> 默认 </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- Spring配置 --> <!-- ====================================== --> <listener> <listenerclass> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/applicationContext.xml</param-value> </context-param> <!--welcome pages--> <welcome-file-list> <welcome-file>pages/index.html</welcome-file> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list>
示例:
SPRING-MVC.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" 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"> <!--启用spring的注解 --> <context:annotation-config/> <!-- 启动定时器 --> <!--<task:annotation-driven/>--> <!-- @controller注解的类是控制器 --> <context:component-scan base-package="com.synergy.*"/> <!--静态资源映射--> <mvc:resources mapping="/statics/css/**" location="/statics/css/"/> <mvc:resources mapping="/statics/js/**" location="/statics/js/"/> <mvc:resources mapping="/statics/img/**" location="/statics/img/"/> <mvc:resources mapping="/statics/plugins/**" location="/statics/plugins/"/> <mvc:resources mapping="/pages/**" location="/pages/"/> <mvc:interceptors> <!--管理页面拦截器 --> <mvc:interceptor> <mvc:mapping path="/pages/management.html"/> <mvc:mapping path="/pages/views/mgr-view/*.html"/> <mvc:mapping path="/mgt/*.action"/> <mvc:exclude-mapping path="/pages/index.html"/> <mvc:exclude-mapping path="/pages/login.html"/> <mvc:exclude-mapping path="/pages/views/overview.html"/> <mvc:exclude-mapping path="/authenUser.action"/> <bean class="com.synergy.filter.MgtViewInterceptor"></bean> </mvc:interceptor> <!--用户页面拦截器 --> <mvc:interceptor> <mvc:mapping path="/pages/console.html"/> <mvc:mapping path="/pages/views/user-view/*.html"/> <mvc:mapping path="/*.action"/> <mvc:exclude-mapping path="/pages/index.html"/> <mvc:exclude-mapping path="/pages/login.html"/> <mvc:exclude-mapping path="/pages/views/overview.html"/> <mvc:exclude-mapping path="/authenUser.action"/> <bean class="com.synergy.filter.UserViewInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> <!-- 注解驱动 将request绑定到controller--> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> </beans>
applicationContext.xml 和 dispatch-servlet.xml形成了两个父子关系的上下文。
1) 一个bean如果在两个文件中都被定义了(比如两个文件中都定义了component scan扫描相同的package), spring会在application context和 servlet context中都生成一个实例,他们处于不同的上下文空间中,他们的行为方式是有可能不一样的。
2) 如果在application context和 servlet context中都存在同一个 @Service 的实例, controller(在servlet context中) 通过 @Resource引用时, 会优先选择servlet context中的实例。
不过最好的方法是:在applicationContext和dispatcher-servlet定义的bean最好不要重复, dispatcher-servlet最好只是定义controller类型的bean。
----------------------------------------------------------------------------------------------------------------------------------------
ApplicationContext.xml 是spring 全局配置文件,用来控制spring 特性的
dispatcher-servlet.xml 是spring mvc里面的,控制器、拦截uri转发view
使用applicationContext.xml文件时是需要在web.xml中添加listener的:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!--使Spring支持自动检测组件,如注解的@Controller --> <context:component-scan base-package="com.parry.test.*" /> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <!-- 数据库配置 --> <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <!-- 测试数据库 --> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3066/TESTDB?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true" /> <property name="username" value="root" /> <property name="password" value="root" /> <!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 --> <property name="idleConnectionTestPeriod" value="240" /> <!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:30,如果要永远存活设置为0 --> <!-- 数据库连接池过期时间应小于等于mysql的过期时间和mycat的过期时间 --> <property name="idleMaxAge" value="20" /> <!-- 每个分区最大的连接数 --> <property name="maxConnectionsPerPartition" value="100" /> <!-- 每个分区最小的连接数 --> <property name="minConnectionsPerPartition" value="20" /> <!-- 分区数 ,默认值2,最小1,推荐3-4,视应用而定 --> <property name="partitionCount" value="1" /> <!-- 每次去拿数据库连接的时候一次性要拿几个,默认值:2 --> <property name="acquireIncrement" value="2" /> <!-- 缓存prepared statements的大小,默认值:0 --> <property name="statementsCacheSize" value="0" /> <property name="connectionTimeoutInMs" value="100" /> <!-- 每个分区释放链接助理进程的数量,默认值:3,除非你的一个数据库连接的时间内做了很多工作,不然过多的助理进程会影响你的性能 --> <property name="releaseHelperThreads" value="3" /> </bean> <!-- 配置SqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis.xml" /> <!-- mapper和resultmap配置路径 --> <property name="mapperLocations"> <list> <!-- 表示在com.sfpay.mapper包或以下所有目录中,以-resultmap.xml结尾所有文件 --> <value>classpath:com/parry/test/dao/config/*.xml</value> </list> </property> </bean> <!-- 配置mapper接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.cn21.calendar.dao" /> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> <!-- 事务配置 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spring bean对象 --> <bean class="com.parry.test.springcontext.SpringContextHolder" lazy-init="false" /> <!-- 定时器 begin --> <!-- 赛程 调度业务对象 --> <bean id="deletePastOrderJob" class="com.parry.test.function.PublicTypeFunction" /> <!-- 赛程 调度业务 --> <bean id="deletePastOrderTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="deletePastOrderJob" /> <property name="targetMethod" value="deletePastOrder" /> </bean> <!-- 赛程 调度器触发器 每天早上07:00执行一次 --> <bean id="deletePastOrderTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="deletePastOrderTask" /> <property name="cronExpression" value="0 13 09 * * ? *" /> </bean> <!-- 设置调度 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <!-- <ref bean="deletePastOrderTaskTrigger" /> --> </list> </property> </bean> <!-- 定时器 end --> <!-- 服务器启动,初始化项目配置参数 --> <bean name="InitalizeBean" class="com.parry.test.configure.impl.InitalizeBean" /> </beans>