如果我们在web项目里面使用spring的话,通常会在web.xml里面配置一个listener.

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

这个litener实现了ServletContextListener接口,并从ContextLoader继承。由于实现了ServletContextListener接口,所以在web容器启动的时候会调用contextInitialized方法。以下是这个方法的实现:

public void contextInitialized(ServletContextEvent event) {

//createContextLoader是一个废弃了的方法,什么也没有做。返回null值

this.contextLoader = createContextLoader();

//所以this.contextLoader为null,这里把自身赋值给contextLoader

//因为ContextLoaderListerner继承了ContextLoader,所以可把自身赋值给

//ContextLoader(这里有点别扭)

if (this.contextLoader == null) {

this.contextLoader = this;

}

//接着就实例化webApplicationContext,由父类ContextLoader实现

this.contextLoader.initWebApplicationContext(event.getServletContext());

}

我们现在来看一下ContextLoader的initWebApplicationContext方法,这个方法比较长,我们分段逐步跟进去看一下。(会省略一些不重要的代码)

以下这段代码主要判断是否重复实例化的问题,因为实例化webApplicationContext后,会把它放到servletContext的一个属性里,所以我们可以从servletContext的属性取出webApplicationContext,如果不为空,则已经实例化,接着就会抛出异常.

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!");

}

接着就会创建webApplicationContext

if (this.context == null) {

this.context = createWebApplicationContext(servletContext);

}

我们跟进去createWebApplicationContext这个方法看一下

protected WebApplicationContext createWebApplicationContext(ServletContext sc)

{

//获取相应的class,其实就是ConfigurableWebApplicationContext.class

Class<?> contextClass = determineContextClass(sc);

//判断是否为 ConfigurableWebApplicationContext.class或从它继承   if(!ConfigurableWebApplicationContext.class.isAssignableFrom(cont         extClass))

{

//若非ConfigurableWebApplicationContext.class或非其子类则抛出异常

throw new ApplicationContextException("Custom context class [" + contextClass.getName() +

"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");

}

//创建一个contextClass的实例对象返回

return(ConfigurableWebApplicationContext)BeanUtils.instantiateClass(contextClass);

}

我们看一下determineContextClass是怎么实现的

protected Class<?> determineContextClass(ServletContext servletContext) {

//从servletContext读取contextClassName

String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);

if (contextClassName != null) {

//如果从servletContext读取到的contextClassName不为空,就返回对应

//的class类

try {

//返回className对应的Class类

return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());

}//如果找不到该类名的类就抛出异常

catch (ClassNotFoundException ex) {

throw new ApplicationContextException(

"Failed to load custom context class [" + contextClassName + "]", ex);

}

}

else {//如果从servletContext读取到得contextClassName为空就取默认的className

contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());

try {//返回className对应的Class类

return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());

}

catch (ClassNotFoundException ex) {

throw new ApplicationContextException(

"Failed to load default context class [" + contextClassName + "]", ex);

}

}

}

至于BeanUtils.instantiateClass(contextClass);

则是通过反射创建对应class的实体对象。

//然后就是把context强制转换为configrableWebApplicationContext

configrableWebApplicationContext cwac = (configrableWebApplicationContext) this.context;

//接着就是核心方法configureAndRefreshWebApplicationContext

configureAndRefreshWebApplicationContext(cwac, servletContext);

//最后把它放到servletContext的属性里

servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

这样webApplicationContext就算加载完成了。

我们现在来看一下核心方法configureAndRefreshWebApplicationContext(cwac, servletContext);(省略一些不重要的代码)

protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {

//把servletContext放到webApplicationContext中,以后可以直接取出来用

wac.setServletContext(sc);

//用户自己的一些设置

customizeContext(sc, wac);

//进行加载

wac.refresh();

}

我们进入webApplicationContext的refresh方法看一下

public void refresh() throws BeansException, IllegalStateException {

synchronized (this.startupShutdownMonitor) {

// Prepare this context for refreshing.

prepareRefresh();

// Tell the subclass to refresh the internal bean factory.

ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

// Prepare the bean factory for use in this context.

prepareBeanFactory(beanFactory);

try {

// Allows post-processing of the bean factory in context subclasses.

postProcessBeanFactory(beanFactory);

// Invoke factory processors registered as beans in the context.

invokeBeanFactoryPostProcessors(beanFactory);

// Register bean processors that intercept bean creation.

registerBeanPostProcessors(beanFactory);

// Initialize message source for this context.

initMessageSource();

// Initialize event multicaster for this context.

initApplicationEventMulticaster();

// Initialize other special beans in specific context subclasses.

onRefresh();

// Check for listener beans and register them.

registerListeners();

// Instantiate all remaining (non-lazy-init) singletons.

finishBeanFactoryInitialization(beanFactory);

// Last step: publish corresponding event.

finishRefresh();

}

catch (BeansException ex) {

// Destroy already created singletons to avoid dangling resources.

destroyBeans();

// Reset 'active' flag.

cancelRefresh(ex);

// Propagate exception to caller.

throw ex;

}

}

}

这是一个同步的方法,这里最核心的方法就是obtainFreshBeanFactory方法。其他的方法都是对webApplicationContext和beanFactory做一些前后的装饰和准备。

我们进入obtainFreshBeanFactoty方法看看

protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {

refreshBeanFactory();

ConfigurableListableBeanFactory beanFactory = getBeanFactory();

if (logger.isDebugEnabled()) {

logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);

}

return beanFactory;

}

这里的核心方法就是refreshBeanFactory();它负责生成BeanFactory并加载bean

protected final void refreshBeanFactory() throws BeansException {

//判断是否已经存在beanFactory如果有则销毁。

if (hasBeanFactory()) {

destroyBeans();

closeBeanFactory();

}

try {

DefaultListableBeanFactory beanFactory = createBeanFactory();//创建一个beanFactory

beanFactory.setSerializationId(getId());//给它一个标识

customizeBeanFactory(beanFactory);//用户自己做一些设置

//这个方法很关键,负责加载所有的bean

loadBeanDefinitions(beanFactory);

synchronized (this.beanFactoryMonitor) {

this.beanFactory = beanFactory;

}

}

catch (IOException ex) {

throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);

}

}

然后我们进入loadBeanDefinitions(beanFactory);看一下

protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {

// Create a new XmlBeanDefinitionReader for the given BeanFactory.

XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

// Configure the bean definition reader with this context's

// resource loading environment.

beanDefinitionReader.setEnvironment(this.getEnvironment());

beanDefinitionReader.setResourceLoader(this);

beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

// Allow a subclass to provide custom initialization of the reader,

// then proceed with actually loading the bean definitions.

initBeanDefinitionReader(beanDefinitionReader);

loadBeanDefinitions(beanDefinitionReader);

}

这里主要工作就是new一个XmlBeanDefinitionReader,给它设置environment,resourceLoader和entityResolver,注意一下由于webApplicationContext实现了ResouceLoader接口,所以它本身就是一个ResourceLoader.

我们可以看到它并不自己去实现lobeanDefinitions方法,而是委托给XmlBeanDefinitionReader去实现。

protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {

String[] configLocations = getConfigLocations();

if (configLocations != null) {

for (String configLocation : configLocations) {

reader.loadBeanDefinitions(configLocation);

}

}

}

这里就对configLocations进行bean的加载,调用重载的方法(spring重载的方法好多啊)

public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {

return loadBeanDefinitions(location, null);

}

public int loadBeanDefinitions(String location, Set<Resource> actualResources) throws BeanDefinitionStoreException {

ResourceLoader resourceLoader = getResourceLoader();

if (resourceLoader == null) {

throw new BeanDefinitionStoreException(

"Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");

}

if (resourceLoader instanceof ResourcePatternResolver) {

// Resource pattern matching available.

try {

Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);

int loadCount = loadBeanDefinitions(resources);

if (actualResources != null) {

for (Resource resource : resources) {

actualResources.add(resource);

}

}

if (logger.isDebugEnabled()) {

logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");

}

return loadCount;

}

catch (IOException ex) {

throw new BeanDefinitionStoreException(

"Could not resolve bean definition resource pattern [" + location + "]", ex);

}

}

else {

// Can only load single resources by absolute URL.

Resource resource = resourceLoader.getResource(location);

int loadCount = loadBeanDefinitions(resource);

if (actualResources != null) {

actualResources.add(resource);

}

if (logger.isDebugEnabled()) {

logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");

}

return loadCount;

}

}

上面这段代码其实就是取得resourceLoader,通过location取得resouces,然后调用

loadBeanDefinitions(resource),这里又是一个重载的方法。

public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {

return loadBeanDefinitions(new EncodedResource(resource));

}

这个方法把resource进行一下编码,再调用一下重载的方法

public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {

Assert.notNull(encodedResource, "EncodedResource must not be null");

if (logger.isInfoEnabled()) {

logger.info("Loading XML bean definitions from " + encodedResource.getResource());

}

Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();

if (currentResources == null) {

currentResources = new HashSet<EncodedResource>(4);

this.resourcesCurrentlyBeingLoaded.set(currentResources);

}

if (!currentResources.add(encodedResource)) {

throw new BeanDefinitionStoreException(

"Detected cyclic loading of " + encodedResource + " - check your import definitions!");

}

try {

InputStream inputStream = encodedResource.getResource().getInputStream();

try {

InputSource inputSource = new InputSource(inputStream);

if (encodedResource.getEncoding() != null) {

inputSource.setEncoding(encodedResource.getEncoding());

}

return doLoadBeanDefinitions(inputSource, encodedResource.getResource());

}

finally {

inputStream.close();

}

}

catch (IOException ex) {

throw new BeanDefinitionStoreException(

"IOException parsing XML document from " + encodedResource.getResource(), ex);

}

finally {

currentResources.remove(encodedResource);

if (currentResources.isEmpty()) {

this.resourcesCurrentlyBeingLoaded.remove();

}

}

}

通过resource取出inpustream,封装一个inputSource,调用doLoadBeanDefinitions(inputSource, encodedResource.getResource());

protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)

throws BeanDefinitionStoreException {

int validationMode = getValidationModeForResource(resource);

Document doc = this.documentLoader.loadDocument(

inputSource, getEntityResolver(), this.errorHandler, validationMode, isNamespaceAware());

return registerBeanDefinitions(doc, resource);

}

这个方法是从resource中读取一个doc对象,值得注意的是,这个doc是w3c的标准。然后进行bean的注册。

registerBeanDefinitions这个方法还没看完。

spring源码读书笔记的更多相关文章

  1. Spring源码学习笔记9——构造器注入及其循环依赖

    Spring源码学习笔记9--构造器注入及其循环依赖 一丶前言 前面我们分析了spring基于字段的和基于set方法注入的原理,但是没有分析第二常用的注入方式(构造器注入)(第一常用字段注入),并且在 ...

  2. Spring 源码学习笔记10——Spring AOP

    Spring 源码学习笔记10--Spring AOP 参考书籍<Spring技术内幕>Spring AOP的实现章节 书有点老,但是里面一些概念还是总结比较到位 源码基于Spring-a ...

  3. Spring 源码学习笔记11——Spring事务

    Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...

  4. Spring源码学习笔记12——总结篇,IOC,Bean的生命周期,三大扩展点

    Spring源码学习笔记12--总结篇,IOC,Bean的生命周期,三大扩展点 参考了Spring 官网文档 https://docs.spring.io/spring-framework/docs/ ...

  5. Spring源码阅读笔记01:源码阅读环境准备

    1. 写在前面 对于做Java开发的同学来说,Spring就像是一条绕不过去的路,但是大多数也只是停留在对Spring的简单使用层面上,对于其背后的原理所知不多也不愿深究,关于这个问题,我在平时的生活 ...

  6. Spring源码阅读笔记

    前言 作为一个Java开发者,工作了几年后,越发觉力有点不从心了,技术的世界实在是太过于辽阔了,接触的东西越多,越感到前所未有的恐慌. 每天捣鼓这个捣鼓那个,结果回过头来,才发现这个也不通,那个也不精 ...

  7. Spring源码阅读笔记02:IOC基本概念

    上篇文章中我们介绍了准备Spring源码阅读环境的两种姿势,接下来,我们就要开始探寻这个著名框架背后的原理.Spring提供的最基本最底层的功能是bean容器,这其实是对IoC思想的应用,在学习Spr ...

  8. spring源码学习笔记之容器的基本实现(一)

    前言 最近学习了<<Spring源码深度解析>>受益匪浅,本博客是对学习内容的一个总结.分享,方便日后自己复习或与一同学习的小伙伴一起探讨之用. 建议与源码配合使用,效果更嘉, ...

  9. Spring源码学习笔记之bean标签属性介绍及作用

    传统的Spring项目, xml 配置bean在代码中是经常遇到, 那么在配置bean的时候,这些属性的作用是什么呢? 虽然说现在boot项目兴起,基于xml配置的少了很多, 但是如果能够了解这些标签 ...

随机推荐

  1. 用selenium 自动爬取某一本小说章节及其内容,并存入数据库中

    from selenium import webdriver import pymysql from selenium.webdriver.support.ui import WebDriverWai ...

  2. 2.Struts2配置文件

    1.配置文件的加载顺序 0. 需要掌握         * 加载了哪些个配置文件(重点的)         * 配置文件的名称是什么         * 配置文件的位置         * 配置文件的 ...

  3. Centos 安装php Imagick 扩展

    从 centos 仓库安装 首先安装 php-pear php-devel,gcc三个软件包 yum install php-pear php-devel gcc 通过 yum 安装Centos 官方 ...

  4. d3操作svg路径动画,及dom移动

    图片跟随路径循环运动,dom也跟着路径运动(利用实时获取坐标位置的方法,改变transform) 1,准备路径 a,自己脑补路径 b,在ps上画好,然后在保存成png-24图片,背景透明,在网站htt ...

  5. prufer序列的性质及相关结论

  6. CGLIB 详解

    依赖 <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> ...

  7. 阿里云重磅推出物联网设备身份认证Link ID²

     2018年12月19日,阿里云宣布推出新版物联网设备身份认证Link ID²及物联网安全运营中心Link SOC,护航万物智联.   随着越来越多的设备连接到网络中,随之而来的安全问题越来越突出. ...

  8. [HL] 7.5 集训总结

    对于某唤做赛区难度的题,我只能是内流满面..拿到题,A神题不可做,B,神题不可做,C,神题不可做...最后yy了一个A的算法...只得了20 TAT.C题骗分似乎有50 ..B题本来想骗分..然后/ ...

  9. Spring-Security (学习记录五)--配置登录时,密码采用md5加密,以及获取登录信息属性监听同步自己想要的登录信息

    目录 1. PasswordEncoder 采用密码加密 2. 获取当前的用户信息 1. PasswordEncoder 采用密码加密 使用前面的例子.可以看出我们数据库密码是采用明文的,我们在登录的 ...

  10. C++的new和delete

    #include <iostream> using namespace std; int main(int argc, char *argv[]) { int *p = NULL; //定 ...