启动Spring项目详细过程(小结)

 更新时间:2019年11月08日 09:48:01   作者:君寞傲  
这篇文章主要介绍了启动Spring项目详细过程(小结),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1、Spring 项目放到web项目容器中(Tomcat、Jetty、JBoss)

本文以通用的Tomcat为例

2、项目容器启动时需要加载读取web.xml配置文件

如下图:

3、容器首先会去读取web.xml配置文件中的两个节点:<listener> </listener>和<context-param> </context-param>

说明:

tomcat在启动web容器的时候会启动一个叫ServletContextListener的监听器,每当在web容器中有ServletContextListener这个接口被实例化的时候,web容器会通知ServletContextListener被实例的对象去执行其contextInitialized()的方法进行相应的业务处理;

而spring框架在设计的过程中ContextLoadListener这个类实现了ServletContextListener这个接口,因此每当有ContextLoadListener这个类被实例化的时候,web容器会通知Spring执行contextInitialized()这个方法,从而进行spring容器的启动与创建的过程中;

4、ContextLoaderListener中的contextInitialized()进行了spring容器的启动配置,调用initWebApplicationContext初始化spring容器;

@Override
public void contextInitialized(ServletContextEvent event) {
  initWebApplicationContext(event.getServletContext());
}
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
  //Spring 启动的句柄,spring容器开始启动的根目录
  if(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
    throw new IllegalStateException("Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!");
  } else {
    Log logger = LogFactory.getLog(ContextLoader.class);
    servletContext.log("Initializing Spring root WebApplicationContext");
    if(logger.isInfoEnabled()) {
      logger.info("Root WebApplicationContext: initialization started");
    }
 
    long startTime = System.currentTimeMillis();
 
    try {
      //处理spring容器是否已经创建(只创建没有创建spring的各个bean)
      if(this.context == null) {
        this.context = this.createWebApplicationContext(servletContext);
      }
 
      if(this.context instanceof ConfigurableWebApplicationContext) {
        ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext)this.context;
        if(!cwac.isActive()) {
          if(cwac.getParent() == null) {
            ApplicationContext parent = this.loadParentContext(servletContext);
            cwac.setParent(parent);
          }
 
          //Spring容器创建完成后,加载spring容器的各个组件
          this.configureAndRefreshWebApplicationContext(cwac, servletContext);
        }
      }
 
      servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
      ClassLoader ccl = Thread.currentThread().getContextClassLoader();
      if(ccl == ContextLoader.class.getClassLoader()) {
        currentContext = this.context;
      } else if(ccl != null) {
        currentContextPerThread.put(ccl, this.context);
      }
 
      if(logger.isDebugEnabled()) {
        logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
      }
 
      if(logger.isInfoEnabled()) {
        long elapsedTime = System.currentTimeMillis() - startTime;
        logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
      }
 
      return this.context;
    } catch (RuntimeException var8) {
      logger.error("Context initialization failed", var8);
      servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, var8);
      throw var8;
    } catch (Error var9) {
      logger.error("Context initialization failed", var9);
      servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, var9);
      throw var9;
    }
  }
}

5、spring容器创建完成后,准备开始实例化加载bean,Spring容器创建完成后,准备向spring容器中加载bean 使用configureAndRefreshWebApplicationContext(cwac, servletContext); 完成bean的加载;

protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {
		if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
			// The application context id is still set to its original default value
			// -> assign a more useful id based on available information
			String idParam = sc.getInitParameter(CONTEXT_ID_PARAM);
			if (idParam != null) {
				wac.setId(idParam);
			}
			else {
				// Generate default id...
				wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
						ObjectUtils.getDisplayString(sc.getContextPath()));
			}
		}
 
		wac.setServletContext(sc);
		String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
		if (configLocationParam != null) {
			wac.setConfigLocation(configLocationParam);
		}
 
		// The wac environment's #initPropertySources will be called in any case when the context
		// is refreshed; do it eagerly here to ensure servlet property sources are in place for
		// use in any post-processing or initialization that occurs below prior to #refresh
		ConfigurableEnvironment env = wac.getEnvironment();
		if (env instanceof ConfigurableWebEnvironment) {
			((ConfigurableWebEnvironment) env).initPropertySources(sc, null);
		}
 
		customizeContext(sc, wac);
		wac.refresh();
	}

说明:

configureAndRefreshWebApplicationContext中加载spring的配置文件,即web.xml中读取<context-param></context-param>中加载到Spring的配置文件,即:classpath:/config/applicationContext.xml;

通过以下代码加载spring配置

public class Application{
 public static void main(String[] args) {
  ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/context.xml");
  ctx.start();
 }
}

此处略过如何调用DefaultResourceLoader

顶级接口ResourceLoader仅提供了一个getResource(String location)方法,可以根据一个资源地址加载资源文件,资源地址的表达式可以是以下几种:

--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
	public Resource getResource(String location) {
		Assert.notNull(location, "Location must not be null");
		for (ProtocolResolver protocolResolver : this.protocolResolvers) {    // 1
			Resource resource = protocolResolver.resolve(location, this);
			if (resource != null) {return resource;}
		}
 
		if (location.startsWith("/")) {return getResourceByPath(location);}    //2
		else if (location.startsWith(CLASSPATH_URL_PREFIX)) {           //3
			return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
		}
		else {
			try {
				// Try to parse the location as a URL...
				URL url = new URL(location);               //4
				return new UrlResource(url);
			}
			catch (MalformedURLException ex) {
				// No URL -> resolve as resource path.
				return getResourceByPath(location);           //5
			}
		}
	}

步骤1,先用扩展协议解析器解析资源地址并返回。举个例子,咱们可以自定义资源解析器来完成带前缀“classpath:”的解析:

首先实现ProtocolResolver接口:

class ClasspathPreProtocolResolver implements ProtocolResolver{
           private static String CLASS_PATH_PRE="classpath:";        
        public Resource resolve(String location, ResourceLoader resourceLoader) {
           if( location.startsWith(CLASS_PATH_PRE)) {
                return new ClassPathResource(location.substring(CLASS_PATH_PRE.length()));
           }       
           return null;
        }        
    }

步骤2,假设location以斜杠开头,则调用该类中 getResourceByPath(String path)方法 ,代码如下:

protected Resource getResourceByPath(String path) {
		return new ClassPathContextResource(path, getClassLoader());
	}

步骤三,假如资源表达式以classpath开头,则截取除前缀calsspath:的路径,并做为ClassPathResource的构造参数,生成ClassPathResource实例后返回。咱们可以在web.xml中做如下配置:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/config/applicationContext.xml</param-value>
</context-param>

6、通过refresh()内部的实现我们大致可以了解整个refresh()方法担负了整个Spring容器初始化和加载的所有逻辑,包括Bean工厂的初始化、post-processor的注册以及调用、bean的实例化、事件发布等。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java实现表白小程序

    Java实现表白小程序

    本文讲述了Java实现表白的代码实例。具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧,具体如下:
    2018-05-05
  • Mybatis中#{}与${}的区别详解

    Mybatis中#{}与${}的区别详解

    这篇文章主要介绍了Mybatis中#{}与${}的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • springboot脱敏控件升级支持深度脱敏

    springboot脱敏控件升级支持深度脱敏

    这篇文章主要为大家介绍了springboot脱敏控件升级支持深度脱敏,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • java线程并发blockingqueue类使用示例

    java线程并发blockingqueue类使用示例

    BlockingQueue是一种特殊的Queue,若BlockingQueue是空的,从BlockingQueue取东西的操作将会被阻断进入等待状态直到BlocingkQueue进了新货才会被唤醒,下面是用BlockingQueue来实现Producer和Consumer的例子
    2014-01-01
  • springboot反爬虫组件kk-anti-reptile的使用方法

    springboot反爬虫组件kk-anti-reptile的使用方法

    这篇文章主要介绍了springboot反爬虫组件kk-anti-reptile的使用方法,帮助大家更好的利用spring boot反爬虫,保护网站安全,感兴趣的朋友可以了解下
    2021-01-01
  • 解决springboot URL带有斜杠的转义字符百分之2F导致的400错误

    解决springboot URL带有斜杠的转义字符百分之2F导致的400错误

    这篇文章主要介绍了解决springboot URL带有斜杠的转义字符百分之2F导致的400错误问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • SpringCloud openfeign声明式服务调用实现方法介绍

    SpringCloud openfeign声明式服务调用实现方法介绍

    在springcloud中,openfeign是取代了feign作为负载均衡组件的,feign最早是netflix提供的,他是一个轻量级的支持RESTful的http服务调用框架,内置了ribbon,而ribbon可以提供负载均衡机制,因此feign可以作为一个负载均衡的远程服务调用框架使用
    2022-12-12
  • 关于logback日志级别动态切换的四种方式

    关于logback日志级别动态切换的四种方式

    这篇文章主要介绍了关于logback日志级别动态切换的四种方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • Spring Bean名称不会被代理的命名技巧

    Spring Bean名称不会被代理的命名技巧

    Spring Bean一些使用小细节就是在不断的源码探索中逐步发现的,今天就来和小伙伴们聊一下通过 beanName 的设置,可以让一个 bean 拒绝被代理
    2023-11-11
  • 基于Java实现Json文件转换为Excel文件

    基于Java实现Json文件转换为Excel文件

    这篇文章主要为大家详细介绍了如何利用Java实现Json文件转换为Excel文件,文中的示例代码讲解详细,具有一定的借鉴价值,需要的可以参考一下
    2022-12-12

最新评论