web.xml是全部web项目的根源,没有它,任何web项目都启动不了,因此有必要了解相关的配置.html
本段引用自 : http://blog.csdn.net/feiyu8607/article/details/6532397java
web.xml中能够有三种方式来配置xml去加载Bean:mysql
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.ContextLoaderServlet
org.springframework.web.servlet.DispatcherServletweb
本段引用自: http://blog.csdn.net/fupengyao/article/details/50605954spring
初始化过程:sql
因此 web.xml的加载过程是context-param >> listener >> fileter >> servlet数据库
web.xml里面能够定义两种参数:
(1)application范围内的参数,存放在servletcontext中,能够在servlet中经过getServletContext().getInitParameter("fruitName");express
在web.xml中配置以下:apache
<context-param> <param-name>fruitName</param-name> <param-value>orange</param-value> </context-param>
(2)servlet范围内的参数,只能在servlet的init()方法中经过this.getInitParameter("fruitName")取得.spring-mvc
在web.xml中配置以下:
<servlet> <servlet-name>PersonServlet</servlet-name> <servlet-class>com.king.servlet.PersonServlet</servlet-class> <init-param> <param-name>fruitName</param-name> <param-value>watermelon</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet>
<!-- application范围内的参数,存放在ServletContext中 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> <!--加入Spring整体配置文件--> classpath:config/applicationContext.xml <!-- /WEB-INF/classes/applicationContext.xml,/WEB-INF/classes/spring-srevlet.xml --> </param-value> </context-param> <!-- Spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
说明都在注释中
<!-- 配置Spring框架自身的拦截器 解决乱码问题 --> <filter> <filter-name>SpringCharacterEncodingFilter</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>SpringCharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
说明都在注释中
<servlet> <!-- DispatcherServlet会默认加载WEB-INF/[DispatcherServlet的Servlet名字]-servlet.xml配置文件 --> <servlet-name>springServlet</servlet-name> <!-- 把全部请求交给Spring Web MVC框架处理 --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- 下面的配置最好直接在一行,且不要有空格,若是输成 "classpath:空格config/applicationContext.xml" By朱青 --> <!-- 将会报错:org.xml.sax.SAXParseException: Content is not allowed in prolog. --> <param-value>classpath:config/spring/springMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <!-- 1)load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法)。 2)它的值必须是一个整数,表示servlet应该被载入的顺序 2)当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet; 3)当值小于0或者没有指定时,则表示容器在该servlet被选择时才会去加载。 4)正数的值越小,该servlet的优先级越高,应用启动时就越先加载。 5)当值相同时,容器就会本身选择顺序来加载。 --> </servlet> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
若是您把上面的段落都仔细阅读完了,会发现<servlet>配置若是<load-on-startup>没有手动配置,那么默认是servlet第一次被访问到才会去初始化的,若是该servlet等web应用启动后,过了好久都没有被访问,那么注释和定时任务都是不会启动的.
并且咱们应当当心listener和servlet重复加载注解引发的启动时间浪费 及 重复加载定时任务引发的数据冲突或不一致.
因此注解,定时任务都建议放在全局监听的<context-param>中,而不建议放在<servlet>的<init-param>中.
在EE web应用的servlet或controller中,可经过WebApplicationContextUtils来获取BeanFactory
BeanFactory factory = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
UserManager userManager = (UserManager)factory.getBean("userManager");
在SE 标准应用中可直接经过java代码来获取BeanFactory
BeanFactory factory = new ClassPathXmlApplictionContext("applicationContext.xml");
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SpringMVC</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <error-page> <error-code>500</error-code> <!-- <exception-type>java.lang.NullPointerException</exception-type> --> <!-- 还有一种配置是指定异常跳转 --> <location>/WEB-INF/jsp/common/errorPage.jsp</location> </error-page> <!-- 基础总配置文件位置 --> <!-- application范围内的参数,存放在ServletContext中 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> <!--加入Spring整体配置文件--> classpath:config/applicationContext.xml <!-- /WEB-INF/classes/applicationContext.xml,/WEB-INF/classes/spring-srevlet.xml --> </param-value> </context-param> <!-- Spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- log4j configuration load --> <servlet> <servlet-name>log4jInit</servlet-name> <servlet-class>config.log.Log4jInit</servlet-class> <init-param> <param-name>log4j-config-file</param-name> <param-value>/WEB-INF/classes/config/log/log4j.properties</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置Spring框架自身的拦截器 解决乱码问题 --> <filter> <filter-name>SpringCharacterEncodingFilter</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>SpringCharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <!-- DispatcherServlet会默认加载WEB-INF/[DispatcherServlet的Servlet名字]-servlet.xml配置文件 --> <servlet-name>springServlet</servlet-name> <!-- 把全部请求交给Spring Web MVC框架处理 --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- 下面的配置最好直接在一行,且不要有空格,若是输成 "classpath:空格config/applicationContext.xml" By朱青 --> <!-- 将会报错:org.xml.sax.SAXParseException: Content is not allowed in prolog. --> <param-value>classpath:config/spring/springMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <!-- 1)load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法)。 2)它的值必须是一个整数,表示servlet应该被载入的顺序 2)当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet; 3)当值小于0或者没有指定时,则表示容器在该servlet被选择时才会去加载。 4)正数的值越小,该servlet的优先级越高,应用启动时就越先加载。 5)当值相同时,容器就会本身选择顺序来加载。 --> </servlet> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- session超时 --> <session-config> <session-timeout>60</session-timeout> </session-config> </web-app>
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:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" > <!-- 启动spring注解,当自动扫描启动后,该配置能够去掉 --> <context:annotation-config /> <!-- 自动扫描 --> <context:component-scan base-package="com.bobo.code" /> <!-- 6. 不一样环境之间切换配置文件,能够读取该配置文件的${test.jdbc.username}表明key去取value , [1]. 在Spring的Bean定义中使用 [2]. 也可用于Spring在java代码中的注解 --> <!-- <context:property-placeholder location="classpath:/config/database/mysql_jdbc.properties" /> --> <context:property-placeholder location="classpath:/config/database/oracle_jdbc_virtual_tele.properties" /> <!--DataBase Configuration --> <!-- Spring的事务管理器有5个,都实现了PlatformTransactionManager接口 DataSourceTransactionManager JDBC事务管理器 HibernateTransactionManager Hibernate事务管理器 JdoTransactionManager JDO事务管理器 JtaTransactionManager JTA事务管理器 PersistenceBrokerTransactionManager Apache的OJB事务管理器 --> <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${dataSource.driverClassName}" /> <property name="url" value="${dataSource.url}" /> <property name="username" value="${dataSource.username}" /> <property name="password" value="${dataSource.password}" /> </bean> <!-- 7. 配置myBatis客户端 --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>classpath:config/mybatis/sqlmap-config.xml</value> </property> <property name="dataSource" ref="dataSource" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 表示事务的开始策略。 propagation="REQUIRED" 表示name的那个方法必需要在一个事务的环境中运行。 read-only="true" 表示只读事务,就是不涉及到数据的修改,只是查询,这是对事务的优化。 --> <!-- 配置事务的传播特性 --> <!-- 8. 配置事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 10. 配置哪些类哪些方法使用事务<aop:pointcut id="allManagerMethod" expression="execution(* com.test.server.dao.*.impl.*(..))"/> --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* com.bobo.code.service.impl.*.*(..))" /> <aop:advisor pointcut-ref="allManagerMethod" advice-ref = "txAdvice"/> </aop:config> <!-- <import resource="classpath*:config/spring/dataSource.xml"/> <import resource="classpath*:config/spring/spring-bean.xml"/> --> <import resource="classpath*:config/spring/timetrigger.xml"/> </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:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- MVC --> <!-- 经过Web.xml的DispatcherServlet加载 --> <!-- 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的 --> <!-- <mvc:annotation-driven /> --> <!-- 2. 组件扫描路径配置,让Spring 容器知道须要扫描哪些包路径下能够加载到容器中的类 --> <!-- 多个扫描路径配置 base-package="com.app,com.core,JUnit4" 也能够写多份,通常直接写多份 --> <context:component-scan base-package="com.bobo.code" /> <!-- 启动spring事务注解, $该启用必须在springMVC中,而不能在applicationContext.xml中配置,否则事务注解无效$ 也就是说只有这一行才能真正开启事务,单独地在类或方法上注解@Transaction只是做了事务标记而以--> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 3. 处理在类级别上的@RequestMapping注解 --> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> <!-- 多个拦截器,顺序执行 --> <!-- <ref bean="SpringMVCInterceptor" /> --> <!-- <ref bean="OpenSessionInViewInterceptor" /> --> </list> </property> </bean> <!-- 4.处理方法级别上的@RequestMapping注解 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="webBindingInitializer"> <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> <property name="conversionService"> <bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean"></bean> </property> </bean> </property> </bean> <!-- 5.对模型视图名称的解析,即给模型视图名称添加先后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/" /> <!-- 让ModelAndView("jsp/teacher/listTeachers.jsp") 从/WEB-INF/目录下开始 --> <property name="suffix" value="" /> <!-- <property name="suffix" value=".jsp" /> --> <!-- Spring内部资源解析类 --> <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView" /> </bean> <!-- 6.异常解析器 --> <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">jsp/common/exception</prop> </props> </property> </bean> </beans>
在个人一个spring 远程方法调用的 测试项目中,xml配置文件只能放在servlet中去访问才能生效,若是放在context中直接报404异常(404表明应该能访问到,可是不存在该servlet地址). 只能猜想,不用该servlet就没法让外部应用访问到该地址,也许地址必须经过servlet的暴露才能访问到.