要随时随地阅读更多技术实战干货,获取项目源代码、学习资料,请关注源代码社区公众号(ydmsq666 )
from:http://Yu Chengyang.iteye.com/blog/1463058
这几天我在看spring的源代码。 与spring启动位置相关的部分,请看spring是从哪里加载的。 本文使用了spring3.0M3
首先,spring加载使用侦听器ContextLoaderListener直接上载到web.xml文件
Xml代码
监听器监听器监听器- class org.spring framework.web.context.contextloaderlistener /监听器- class /监听器
通常,统一管理加载位置
Xml代码
上下文- param param-namecontextconfiglocation/param-name param-value/we B- INF/conf/spring/* */*.XML/par aram
此org.springframework.web.context.contextloaderlistener类型是spring framework原始加载上下文的监听程序。
通常,自定义监听器以继承ContextLoaderListener,并单独实现需要初始化的接口。 通常,您会选择实现一些接口来管理会话。)
Java代码
pblicclassframeservletcontextlistenerextendscontextloaderlistenerimplementsservletcontextlistener, httpsessionattributelistener http session listener (//privateservletcontextinitpath (servletcontexteventevent ) ) publicsynchon
设置监听程序后,启动web容器监听程序并开始启动ContextLoaderListenerl
类中的方法上下文初始化(
Java代码
/* * initializetherootwebapplicationcontext.*/publicvoidcontextinitialized (servletcontexteventevent ) this.contex ext ial } this.context loader.initwebapplicationcontext (event.getservletcontext ) ); }
这样this.context loader.initwebapplicationcontext (event.getservletcontext ) ); 上下文角色列表器
使用容器的上下文初始化spring的APP应用程序上下文,并使用名为ContextLoader的类
初始化上下文加载器时显示了这样的静态代码
Java代码
静态//loaddefaultstrategyimplementationsfrompropertiesfile.//thisiscurrentlystrictlyinternalandnotmeantobecustomization byapplicationdevelopers.try {//一文为, 此软件包下的单个properties文件的值(ContextLoader.properties ) classpathresourceresource=new work default strategies=properties }catch(ioexceptionex ) thrownewillegalstateexception (' could not load ' context loader.properties ' : ' ex.getmesssion
在属性文件中这样定义
引用
org.spring framework.web.context.webapplicationcontext=org.spring framework.web.context.support.xmlwebaplication
这样,可以根据属性文件的定义反映XmlWebApplicationContext上下文
但是,在XmlWebApplicationContext中可以看到以下变量
Java代码
/* * defaultconfiglocationfortherootcontext */publicstaticfinalstringdefault _ config _ location='/we B- INF/applicaricatic
现在,您知道了默认情况下加载spring文件的启动位置
查看上下文加载器类,可以看到传说中的参数contextConfigLocation
Java代码
publicstaticfinalstringconfig _ location _ param=' contextconfiglocation ';
XmlWebApplicationContext对象通过调用此参数设置启动位置
Java代码
WAC.set config location (servlet context.getinitparameter ) config_location_param );
此外,XmlWebApplicationContext继承的abstractrefreshableconfigapplicationcontext类的setConfigLocation方法是此抽象类的字符串[ ] cconfig
此外,abstractrefreshableconfigapplicationcontext类提供了对默认启动文件位置和配置启动文件位置的spring支持
Java代码
保护性字符串[ ] getconfig locations () return ) this.configlocations!=null? this.config locations 3360 getdefaultconfiglocations (); }
现在,您知道了spring从哪里加载,以及将加载哪个文件。