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
- ContextLoaderListener 和 ContextLoaderServlet : 本质上是等同的,都是调用ContextLoader来加载web程序的上下文,加载完成以后,都是在ServletContext中,只不过listener需要Servlet2.3及以上支持。
- 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
初始化过程:
- 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点
和 。 - 接着容器会创建一个ServletContext(上下文),应用范围内即整个WEB项目都能使用这个上下文。
- 接着容器会将读取到
转化为键值对,并交给ServletContext。 - 容器创建
中的类实例,即创建监听(备注:listener定义的类可以是自定义的类但必须需要继承ServletContextListener)。 - 在监听的类中会有一个contextInitialized(ServletContextEvent event)初始化方法,在这个方法中可以通过event.getServletContext().getInitParameter("contextConfigLocation") 来得到context-param 设定的值。在这个类中还必须有一个contextDestroyed(ServletContextEvent event) 销毁方法.用于关闭应用前释放资源,比如说数据库连接的关闭。
- 得到这个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>fruitNameparam-name> <param-value>orangeparam-value> context-param>
(2)servlet范围内的参数,只能在servlet的init()方法中通过this.getInitParameter("fruitName")取得.
在web.xml中配置如下:
<servlet> <servlet-name>PersonServletservlet-name> <servlet-class>com.king.servlet.PersonServletservlet-class> <init-param> <param-name>fruitNameparam-name> <param-value>watermelonparam-value> init-param> <load-on-startup>0load-on-startup> servlet>
ContextLoderListener配置
contextConfigLocation 为固定写法.中可以通过classpath或/WEB-INF 两种路径来加载xml文件.
<context-param> <param-name>contextConfigLocationparam-name> <param-value> classpath:config/applicationContext.xml param-value> context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class> listener>
filter配置
说明都在注释中
<filter> <filter-name>SpringCharacterEncodingFilterfilter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class> <init-param> <param-name>encodingparam-name> <param-value>UTF-8param-value> init-param> <init-param> <param-name>forceEncodingparam-name> <param-value>trueparam-value> init-param> filter> <filter-mapping> <filter-name>SpringCharacterEncodingFilterfilter-name> <url-pattern>/*url-pattern> filter-mapping>
servlet配置
说明都在注释中
<servlet> <servlet-name>springServletservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <init-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:config/spring/springMVC.xmlparam-value> init-param> <load-on-startup>1load-on-startup> servlet> <servlet-mapping> <servlet-name>springServletservlet-name> <url-pattern>*.dourl-pattern> servlet-mapping>
注解,定时任务等xml配置在哪加载合适?
如果您把上面的段落都仔细阅读完了,会发现
而且我们应当小心listener和servlet重复加载注解引起的启动时间浪费 及 重复加载定时任务引起的数据冲突或不一致.
所以注解,定时任务都建议放在全局监听的
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>SpringMVCdisplay-name> <welcome-file-list> <welcome-file>index.htmlwelcome-file> <welcome-file>index.jspwelcome-file> welcome-file-list> <error-page> <error-code>500error-code> <location>/WEB-INF/jsp/common/errorPage.jsplocation> error-page> <context-param> <param-name>contextConfigLocationparam-name> <param-value> classpath:config/applicationContext.xml param-value> context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class> listener> <servlet> <servlet-name>log4jInitservlet-name> <servlet-class>config.log.Log4jInitservlet-class> <init-param> <param-name>log4j-config-fileparam-name> <param-value>/WEB-INF/classes/config/log/log4j.propertiesparam-value> init-param> <load-on-startup>1load-on-startup> servlet> <filter> <filter-name>SpringCharacterEncodingFilterfilter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class> <init-param> <param-name>encodingparam-name> <param-value>UTF-8param-value> init-param> <init-param> <param-name>forceEncodingparam-name> <param-value>trueparam-value> init-param> filter> <filter-mapping> <filter-name>SpringCharacterEncodingFilterfilter-name> <url-pattern>/*url-pattern> filter-mapping> <servlet> <servlet-name>springServletservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <init-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:config/spring/springMVC.xmlparam-value> init-param> <load-on-startup>1load-on-startup> servlet> <servlet-mapping> <servlet-name>springServletservlet-name> <url-pattern>*.dourl-pattern> servlet-mapping> <session-config> <session-timeout>60session-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" > <context:annotation-config /> <context:component-scan base-package="com.bobo.code" /> <context:property-placeholder location="classpath:/config/database/oracle_jdbc_virtual_tele.properties" /> <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> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>classpath:config/mybatis/sqlmap-config.xmlvalue> property> <property name="dataSource" ref="dataSource" /> bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" /> tx:attributes> tx:advice> <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/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"> <context:component-scan base-package="com.bobo.code" /> <tx:annotation-driven transaction-manager="transactionManager" /> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> list> property> bean> <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> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/" /> <property name="suffix" value="" /> <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView" /> bean> <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">jsp/common/exceptionprop> props> property> bean> beans>