启动Spring项目详细过程(小结)
1、Spring项目放到web项目容器中(Tomcat、Jetty、JBoss)
本文以通用的Tomcat为例
2、项目容器启动时需要加载读取web.xml配置文件
如下图:
3、容器首先会去读取web.xml配置文件中的两个节点:
说明:
tomcat在启动web容器的时候会启动一个叫ServletContextListener的监听器,每当在web容器中有ServletContextListener这个接口被实例化的时候,web容器会通知ServletContextListener被实例的对象去执行其contextInitialized()的方法进行相应的业务处理;
而spring框架在设计的过程中ContextLoadListener这个类实现了ServletContextListener这个接口,因此每当有ContextLoadListener这个类被实例化的时候,web容器会通知Spring执行contextInitialized()这个方法,从而进行spring容器的启动与创建的过程中;
4、ContextLoaderListener中的contextInitialized()进行了spring容器的启动配置,调用initWebApplicationContext初始化spring容器;
@Override
publicvoidcontextInitialized(ServletContextEventevent){
initWebApplicationContext(event.getServletContext());
}
publicWebApplicationContextinitWebApplicationContext(ServletContextservletContext){
//Spring启动的句柄,spring容器开始启动的根目录
if(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)!=null){
thrownewIllegalStateException("Cannotinitializecontextbecausethereisalreadyarootapplicationcontextpresent-checkwhetheryouhavemultipleContextLoader*definitionsinyourweb.xml!");
}else{
Loglogger=LogFactory.getLog(ContextLoader.class);
servletContext.log("InitializingSpringrootWebApplicationContext");
if(logger.isInfoEnabled()){
logger.info("RootWebApplicationContext:initializationstarted");
}
longstartTime=System.currentTimeMillis();
try{
//处理spring容器是否已经创建(只创建没有创建spring的各个bean)
if(this.context==null){
this.context=this.createWebApplicationContext(servletContext);
}
if(this.contextinstanceofConfigurableWebApplicationContext){
ConfigurableWebApplicationContextcwac=(ConfigurableWebApplicationContext)this.context;
if(!cwac.isActive()){
if(cwac.getParent()==null){
ApplicationContextparent=this.loadParentContext(servletContext);
cwac.setParent(parent);
}
//Spring容器创建完成后,加载spring容器的各个组件
this.configureAndRefreshWebApplicationContext(cwac,servletContext);
}
}
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,this.context);
ClassLoaderccl=Thread.currentThread().getContextClassLoader();
if(ccl==ContextLoader.class.getClassLoader()){
currentContext=this.context;
}elseif(ccl!=null){
currentContextPerThread.put(ccl,this.context);
}
if(logger.isDebugEnabled()){
logger.debug("PublishedrootWebApplicationContextasServletContextattributewithname["+WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE+"]");
}
if(logger.isInfoEnabled()){
longelapsedTime=System.currentTimeMillis()-startTime;
logger.info("RootWebApplicationContext:initializationcompletedin"+elapsedTime+"ms");
}
returnthis.context;
}catch(RuntimeExceptionvar8){
logger.error("Contextinitializationfailed",var8);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,var8);
throwvar8;
}catch(Errorvar9){
logger.error("Contextinitializationfailed",var9);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,var9);
throwvar9;
}
}
}
5、spring容器创建完成后,准备开始实例化加载bean,Spring容器创建完成后,准备向spring容器中加载bean使用configureAndRefreshWebApplicationContext(cwac,servletContext);完成bean的加载;
protectedvoidconfigureAndRefreshWebApplicationContext(ConfigurableWebApplicationContextwac,ServletContextsc){
if(ObjectUtils.identityToString(wac).equals(wac.getId())){
//Theapplicationcontextidisstillsettoitsoriginaldefaultvalue
//->assignamoreusefulidbasedonavailableinformation
StringidParam=sc.getInitParameter(CONTEXT_ID_PARAM);
if(idParam!=null){
wac.setId(idParam);
}
else{
//Generatedefaultid...
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX+
ObjectUtils.getDisplayString(sc.getContextPath()));
}
}
wac.setServletContext(sc);
StringconfigLocationParam=sc.getInitParameter(CONFIG_LOCATION_PARAM);
if(configLocationParam!=null){
wac.setConfigLocation(configLocationParam);
}
//Thewacenvironment's#initPropertySourceswillbecalledinanycasewhenthecontext
//isrefreshed;doiteagerlyheretoensureservletpropertysourcesareinplacefor
//useinanypost-processingorinitializationthatoccursbelowpriorto#refresh
ConfigurableEnvironmentenv=wac.getEnvironment();
if(envinstanceofConfigurableWebEnvironment){
((ConfigurableWebEnvironment)env).initPropertySources(sc,null);
}
customizeContext(sc,wac);
wac.refresh();
}
说明:
configureAndRefreshWebApplicationContext中加载spring的配置文件,即web.xml中读取
或
通过以下代码加载spring配置
publicclassApplication{
publicstaticvoidmain(String[]args){
ClassPathXmlApplicationContextctx=newClassPathXmlApplicationContext("/context.xml");
ctx.start();
}
}
此处略过如何调用DefaultResourceLoader
顶级接口ResourceLoader仅提供了一个getResource(Stringlocation)方法,可以根据一个资源地址加载资源文件,资源地址的表达式可以是以下几种:
--1.classpath:前缀开头的表达式,例如:classpath:smart-context.xml
--2.“/”开头的表达式,例如:/WEB-INF/classes/smart-context.xml
--3.非“/”开头的表达,例如:WEB-INF/classes/smart-context.xml
--4.url协议,例如:file:/D:/ALANWANG-AIA/Horse-workspace/chapter3/target/classes/smart-context.xml
Spring提供了实现类DefaultResourceLoader,DefaultResourceLoader在实现了以上列举的功能基础上,还为开发者提供了自定义扩展接口ProtocolResolver,开发者可实现该接口定制个性化资源表达式,代码如下:
@Override
publicResourcegetResource(Stringlocation){
Assert.notNull(location,"Locationmustnotbenull");
for(ProtocolResolverprotocolResolver:this.protocolResolvers){//1
Resourceresource=protocolResolver.resolve(location,this);
if(resource!=null){returnresource;}
}
if(location.startsWith("/")){returngetResourceByPath(location);}//2
elseif(location.startsWith(CLASSPATH_URL_PREFIX)){//3
returnnewClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()),getClassLoader());
}
else{
try{
//TrytoparsethelocationasaURL...
URLurl=newURL(location);//4
returnnewUrlResource(url);
}
catch(MalformedURLExceptionex){
//NoURL->resolveasresourcepath.
returngetResourceByPath(location);//5
}
}
}
步骤1,先用扩展协议解析器解析资源地址并返回。举个例子,咱们可以自定义资源解析器来完成带前缀“classpath:”的解析:
首先实现ProtocolResolver接口:
classClasspathPreProtocolResolverimplementsProtocolResolver{
privatestaticStringCLASS_PATH_PRE="classpath:";
publicResourceresolve(Stringlocation,ResourceLoaderresourceLoader){
if(location.startsWith(CLASS_PATH_PRE)){
returnnewClassPathResource(location.substring(CLASS_PATH_PRE.length()));
}
returnnull;
}
}
步骤2,假设location以斜杠开头,则调用该类中getResourceByPath(Stringpath)方法,代码如下:
protectedResourcegetResourceByPath(Stringpath){
returnnewClassPathContextResource(path,getClassLoader());
}
步骤三,假如资源表达式以classpath开头,则截取除前缀calsspath:的路径,并做为ClassPathResource的构造参数,生成ClassPathResource实例后返回。咱们可以在web.xml中做如下配置:
contextConfigLocation classpath:/config/applicationContext.xml
6、通过refresh()内部的实现我们大致可以了解整个refresh()方法担负了整个Spring容器初始化和加载的所有逻辑,包括Bean工厂的初始化、post-processor的注册以及调用、bean的实例化、事件发布等。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。