Web项目使用Spring框架服务器启动加载xml文件的过程学习

个人在Tomcat简单部署了一个web项目,console启动日志。

2015-9-13 20:36:39 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files (x86)\Java\jdk1.6.0_45\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\DMIX;D:\Program Files\TortoiseSVN\bin;C:\Program Files\Java\jdk1.7.0_79\bin;D:\wls1036_dev\wlserver\common\bin;D:\Program Files (x86)\instantclient_11_2;.
2015-9-13 20:36:40 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:helloworld' did not find a matching property.
2015-9-13 20:36:40 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:springmvc' did not find a matching property.
2015-9-13 20:36:41 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2015-9-13 20:36:41 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 2786 ms
2015-9-13 20:36:41 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2015-9-13 20:36:41 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.44
2015-9-13 20:36:42 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'hello'
2015-9-13 20:36:42 org.springframework.web.servlet.DispatcherServlet initServletBean
信息: FrameworkServlet 'hello': initialization started
2015-9-13 20:36:43 org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
信息: Refreshing WebApplicationContext for namespace 'hello-servlet': startup date [Sun Sep 13 20:36:42 CST 2015]; root of context hierarchy
2015-9-13 20:36:43 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from ServletContext resource [/WEB-INF/hello-servlet.xml]
2015-9-13 20:36:43 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@83e1e: defining beans [welcomeController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
2015-9-13 20:36:44 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
信息: Mapped URL path [/hello] onto handler 'welcomeController'
2015-9-13 20:36:44 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
信息: Mapped URL path [/hello.*] onto handler 'welcomeController'
2015-9-13 20:36:44 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
信息: Mapped URL path [/hello/] onto handler 'welcomeController'
2015-9-13 20:36:44 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
信息: Mapped URL path [/welcome] onto handler 'welcomeController'
2015-9-13 20:36:44 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
信息: Mapped URL path [/welcome.*] onto handler 'welcomeController'
2015-9-13 20:36:44 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
信息: Mapped URL path [/welcome/] onto handler 'welcomeController'
2015-9-13 20:36:44 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
信息: Mapped URL path [/welcome.html] onto handler 'welcomeController'
2015-9-13 20:36:44 org.springframework.web.servlet.DispatcherServlet initServletBean
信息: FrameworkServlet 'hello': initialization completed in 1830 ms
2015-9-13 20:36:44 org.apache.catalina.startup.HostConfig deployDescriptor
信息: Deploying configuration descriptor host-manager.xml
2015-9-13 20:36:44 org.apache.catalina.startup.HostConfig deployDescriptor
信息: Deploying configuration descriptor manager.xml
2015-9-13 20:36:44 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory docs
2015-9-13 20:36:44 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory examples
2015-9-13 20:36:45 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
2015-9-13 20:36:45 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
2015-9-13 20:36:45 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory ROOT
2015-9-13 20:36:45 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2015-9-13 20:36:45 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2015-9-13 20:36:45 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/53  config=null
2015-9-13 20:36:45 org.apache.catalina.startup.Catalina start
信息: Server startup in 4039 ms
2015-9-13 20:39:28 org.springframework.web.servlet.PageNotFound noHandlerFound
警告: No mapping found for HTTP request with URI [/springmvc/welcome1] in DispatcherServlet with name 'hello'


spring框架版本:spring-framework-3.2.9.RELEASE

首先,说明一下console日志的特殊之处,下面两行是一条日志。时间+类名+方法名+日志级别+提示内容(这个日志的配置文件我还没来得及查找其位置,可能是Tomcat或Spring框架的jar包中,后期找到也分享出来)

了解控制台日志格式,对理解整个过程很有帮助。

2015-9-13 20:36:42 org.springframework.web.servlet.DispatcherServlet initServletBean
信息: FrameworkServlet 'hello': initialization started

下面摘自:http://blog.csdn.net/zhulinu/article/details/7305247  写得很不错

ApplicationContext是Spring的核心,Context我们通常解释为上下文环境,我想用“容器”来表述它更容易理解一些,ApplicationContext则是“应用的容器”了;在Web应用中,我们会用到WebApplicationContext,WebApplicationContext继承自ApplicationContext;WebApplicationContext的初始化方式和BeanFactory.ApplicationContext有所区别,因为WebApplicationContext需要ServletContext实例,也就是说它必须拥有Web容器的前提下才能完成启动的工作.有过Web开发经验的读者都知道可以在web.xml中配置自启动的Servlet或定义Web容器监听器(ServletContextListener),借助着两者中的任何一个,我们就可以启动Spring Web应用上下文的工作.

Spring分别提供了用于启动WebApplicationContext的Servlet和Web容器监听器:

org.springframework.web.context.ContextLoaderServlet;

org.springframework.web.context.ContextLoaderListener.

这两个方法都是在web应用启动的时候来初始化WebApplicationContext,我个人认为Listerner要比Servlet更好一些,因为Listerner监听应用的启动和结束,而Servlet得启动要稍微延迟一些,如果在这时要做一些业务的操作,启动的前后顺序是有影响的。

配置例子如下:
context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value>/WEB-INF/applicationContext.xml</param-value> 
</context-param> 

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

那么在ContextLoaderListener和ContextLoaderServlet中到底做了什么呢? 
以ContextLoaderListener为例,我们可以看到 
public void contextInitialized(ServletContextEvent event) { 
this.contextLoader = createContextLoader(); 
this.contextLoader.initWebApplicationContext(event.getServletContext()); 

protected ContextLoader createContextLoader() { 
return new ContextLoader(); 

ContextLoader是一个工具类,用来初始化WebApplicationContext,其主要方法就是initWebApplicationContext,我们继续追踪initWebApplicationContext这个方法(具体代码我不贴出,大家可以看Spring中的源码),我们发现,原来ContextLoader是把WebApplicationContext(XmlWebApplicationContext是默认实现类)放在了ServletContext中,ServletContext也是一个“容器”,也是一个类似Map的结构,而WebApplicationContext在ServletContext中的KEY就是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,我们如果要使用WebApplicationContext则需要从ServletContext取出,Spring提供了一个WebApplicationContextUtils类,可以方便的取出WebApplicationContext,只要把ServletContext传入就可以了。 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值