web.xml

web.xml是所有web项目的根源,没有它,任何web项目都启动不了,所以有必要了解相关的配置.

ContextLoderListener,ContextLoaderServlet,DispatcherServlet 区别

本段引用自 : http://blog.csdn.net/feiyu8607/article/details/6532397

web.xml中可以有三种方式来配置xml去加载Bean:

org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.ContextLoaderServlet
org.springframework.web.servlet.DispatcherServlet

  1. ContextLoaderListener 和 ContextLoaderServlet : 本质上是等同的,都是调用ContextLoader来加载web程序的上下文,加载完成以后,都是在ServletContext中,只不过listener需要Servlet2.3及以上支持。
  2. ContextLoaderListene与DispatcherServlet : 用DispatcherServlet载入的Bean是隶属于此Servlet的(所以spring可以配置多个分别拥有各自环境的DispatcherServlet),因此其他servlet无法获取到该Context。这一现象在buffalo配置时曾经出现(无法找到服务bean)。分析了buffalo和spring的源码后,将xml在ContextLoaderListener配置才得以解决。   所以web.xml文件中若只使用了一个dispatcherservlet来进行分发,则使用dispatcherservlet contextloaderlistener 来加载bean实例是等效的。

各元素初始化过程

本段引用自: http://blog.csdn.net/fupengyao/article/details/50605954

初始化过程:

  1. 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点<listener>和<contex-param>。
  2. 接着容器会创建一个ServletContext(上下文),应用范围内即整个WEB项目都能使用这个上下文。
  3. 接着容器会将读取到<context-param>转化为键值对,并交给ServletContext。
  4. 容器创建<listener></listener>中的类实例,即创建监听(备注:listener定义的类可以是自定义的类但必须需要继承ServletContextListener)。
  5. 在监听的类中会有一个contextInitialized(ServletContextEvent event)初始化方法,在这个方法中可以通过event.getServletContext().getInitParameter("contextConfigLocation") 来得到context-param 设定的值。在这个类中还必须有一个contextDestroyed(ServletContextEvent event) 销毁方法.用于关闭应用前释放资源,比如说数据库连接的关闭。
  6. 得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早。

所以 web.xml的加载过程是context-param >> listener  >> fileter  >> servlet

context-param和init-param区别

web.xml里面可以定义两种参数:
(1)application范围内的参数,存放在servletcontext中,可以在servlet中通过getServletContext().getInitParameter("fruitName");

在web.xml中配置如下:

<context-param>
<param-name>fruitName</param-name>
<param-value>orange</param-value>
</context-param>

(2)servlet范围内的参数,只能在servlet的init()方法中通过this.getInitParameter("fruitName")取得.

在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>

ContextLoderListener配置

  1. <param-name>contextConfigLocation</param-name>为固定写法.
  2. <param-value></param-value>中可以通过classpath或/WEB-INF 两种路径来加载xml文件.
<!-- 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>

filter配置

说明都在注释中

<!--  配置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配置

说明都在注释中

    <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>

注解,定时任务等xml配置在哪加载合适?

如果您把上面的段落都仔细阅读完了,会发现<servlet>配置如果<load-on-startup>没有手动配置,那么默认是servlet第一次被访问到才会去初始化的,如果该servlet等web应用启动后,过了很久都没有被访问,那么注释和定时任务都是不会启动的.

而且我们应当小心listener和servlet重复加载注解引起的启动时间浪费  及  重复加载定时任务引起的数据冲突或不一致.

所以注解,定时任务都建议放在全局监听的<context-param>中,而不建议放在<servlet>的<init-param>中.

BeanFactory获取

在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,applicationContext.xml,springMVC.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的暴露才能访问到.

spring web.xml 难点配置总结的更多相关文章

  1. spring web.xml 难点配置总结【转】

    web.xml web.xml是所有web项目的根源,没有它,任何web项目都启动不了,所以有必要了解相关的配置. ContextLoderListener,ContextLoaderServlet, ...

  2. spring web.xml基本配置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmln ...

  3. web.xml中配置Spring中applicationContext.xml的方式

    2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...

  4. Spring Web工程web.xml零配置即使用Java Config + Annotation

    摘要: 在Spring 3.0之前,我们工程中常用Bean都是通过XML形式的文件注解的,少了还可以,但是数量多,关系复杂到后期就很难维护了,所以在3.x之后Spring官方推荐使用Java Conf ...

  5. Spring整合Hibernate的XML文件配置,以及web.xml文件配置

    利用Spring整合Hibernate时的XML文件配置 applicationContext.xml <?xml version="1.0" encoding=" ...

  6. SSH web.xml文件配置

    启动一个WEB项目的时候, WEB容器会去读取它的配置文件web.xml web.xml中配置的加载优先级:context-param -> listener -> filter -> ...

  7. struts2在web.xml中配置详情

    web.xml是web应用中载入有关servlet信息的重要配置文件,起着初始化servlet,filter等web程序的作用. 通常,全部的MVC框架都须要Web应用载入一个核心控制器.那採取什么方 ...

  8. springmvc 项目完整示例07 设置配置整合springmvc springmvc所需jar包springmvc web.xml文件配置

    前面主要是后台代码,spring以及mybatis的整合 下面主要是springmvc用来处理请求转发,展现层的处理 之前所有做到的,完成了后台,业务层和持久层的开发完成了 接下来就是展现层了 有很多 ...

  9. struts2 在 Action 或 Interceptor 中获取 web.xml 中配置的 <context-param> 参数 (这是我的第一篇博文,哈哈。)

    最近为了改一个问题,想加一个控制开关,就在web.xml 中配置了一个 <context-param> 参数,并在 Action 或 Interceptor 中获取参数值. 1.在 web ...

随机推荐

  1. vim学习

    vim编辑器的工作模式分为3种 1.Command Mode 命令模式 2.Insert Mode 插入模式 3.Lastline Mode 底行模式 vim 打开文件时处于命令模式,i 可以切换到插 ...

  2. Google App Engine, Python2.7的UnicodeDecodeError bug

    在跟Web Development,要在Google App Engine上写作业,出师不利,遇到以下bug: 2014-05-06 16:14:17 Running command: "[ ...

  3. delphi.指针.PChar

    此文是delphi.指针.应用姊妹篇,想细化一下PChar应用,所以有了此文. 注意: 1:此文讲的是PChar与字符串相关操作,其它方法暂不多讲. 2:由于D分开Ansi/Unicode的两种完全不 ...

  4. MVC中使用内建的HTML辅助方法产生表单元素提交表单与button按钮事件的陷阱

    网站模板页有个登陆的退出按钮,当点击时跳转到登陆页面. <button onclick="logout()" >退出</button> $("#l ...

  5. OGC学习课程

    1.引言 由于项目需要,需要学习OGC相关地图标准,包括WMS.WFS.GML.SLD等,只是国内相关书籍大家都懂的,特向Google大师请教,得一秘籍<Open Web Mapping> ...

  6. android解析图片资源缩小放大问题

    今天突然发现,图片放在drawable 不同分辨率的目录下会有不同程度的放大或者缩小?这是为什么呢? 首先我们从decodeResource()方法入手 public static Bitmap de ...

  7. 【SSM 7】Mybatis底层封装思路

    一.基本概述 在前面的博客中介绍到Mybatis的逆向生成工具,为我们生成了每个实体的基本增删改查的代码,那么每个实体都是那么多的代码,我们很容易的发现,有很大的相似性.对于这部分代码,应该予以抽象封 ...

  8. struts2原理理解

    1.  由容器创建HttpServletRequest请求,这个请求经过一系列的过滤器,最终到struts2的核心过滤器(FilterDispatch), 2.  核心过滤器会根据url请求获得Act ...

  9. php中cookie技术关于跨目录调用cookie值的问题

    今天做项目发现了一个奇葩错误,以cookie技术为主,反复测试发现cookie不能跨目录调用. 我在F:wamp\www\test\下面有1.php和2.php其中1.php接受2.php中setco ...

  10. Geometry shader总结

    什么是Geometry Shader GS存在于vertext shader和固定功能vertex post-processing stage之间,它是可选的不是必要的.GS的输入是单个primiti ...