Spring源码学习一

前言

学完spring,mybatis,springmvc,spring-boot基础用法后,感觉还是浮在表面,所以还是想要从源码出发,独自分析他的运作流程,本文章系列所有都是自己通过源码分析,自己的理解,所以可能会有一系列的不准确,或者错误,希望可以有大神指正出来

Spring 分析一 加载前的准备

例子:

分析先从IOC控制翻转分析,写出简易例子,之后通过debug查看源码分析其中流程
总体预览

在这里插入图片描述

FeiSay类
public class FeiSay {
    public String say(String word){
        return "fei say: "+word;
    }
}
Test类
package com.fei;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("fei.xml");
        FeiSay bean = (FeiSay)context.getBean("feiSay", "FeiSay");
        System.out.println(bean.say("feige011 is back"));
    }
}
fei.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- services -->

    <bean id="feiSay" class="com.fei.FeiSay">
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions for services go here -->

</beans>

通过debug第一行

ApplicationContext context = new ClassPathXmlApplicationContext("fei.xml");

Spring 中ClassPathXmlApplicationContext执行方法前执行的静态方法

发现Spring会有加载前的准备
准备有3个

//由spring.spel.ignore系统属性控制的bool,该属性指示spring忽略spel,即不初始化spel基础结构。
private static final boolean shouldIgnoreSpel = SpringProperties.getFlag("spring.spel.ignore");
//此环境是否位于本机映像中。作为私有静态字段而不是在NativeImageDetector.inNativeImage()静态方法中另见https://github.com/oracle/graal/issues/2594.
private static final boolean IN_NATIVE_IMAGE =  (System.getProperty("org.graalvm.nativeimage.imagecode") != null);
static {
		// Eagerly load the ContextClosedEvent class to avoid weird classloader issues
		// on application shutdown in WebLogic 8.1. (Reported by Dustin Woods.)
		//急切地加载ContextClosedEvent类以避免奇怪的类加载器问题//在WebLogic 8.1中关闭应用程序时。(达斯汀·伍兹报道。)
		ContextClosedEvent.class.getName();
	}
	

我们主要分析shouldIgnoreSpel这个属性
我们分析通过手动画图,一点点给他们连接起来

当我们从顺序从上往下缕的时候很容易绕蒙,一定要结合图来看。

在这里插入图片描述
我将一点点解释我画的这张图
首先shouldIgnoreSpel通过**SpringProperties.getFlag(“spring.spel.ignore”);**来获取,那么就开始查看这个SpringProperties.getFlag方法,

public static boolean getFlag(String key) {
//key为"spring.spel.ignore"
	return Boolean.parseBoolean(getProperty(key));
}

使用的是本身的SpringProperties.getProperty方法,然后进入第三个图

/*
检索给定键的属性值,首先检查本地Spring属性,然后返回到JVM级别的系统属性。
*/
@Nullable
public staticString getProperty(String key) {
	String value = localProperties.getProperty(key);  //检查本地的Spring属性
	if (value == null) {
		try {
			value = System.getProperty(key);  //检索JVM级别的系统属性
		}
		catch (Throwable ex) {
			System.err.println("Could not retrieve system property '" + key + "': " + ex);
		}
	}
	return value; //在本流程中,发现没搜到,返回null
}

本图代码未知量
localProperties是什么?
在SpringProperties下

private static final Properties localProperties = new Properties();

所以发现
localPropertiesgetProperty(key)方法是Properties类下的
Properties.getProperty(key);

public String getProperty(String key) {
       Object oval = super.get(key);  //调用get方法 
       String sval = (oval instanceof String) ? (String)oval : null;//Obejct转换String类型
       return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval; //defaults!=null时,返回defaults.getProperty(key)否则返回sval
   }

出现两个未知数据super.get(key),和defaults
super.get(key)

class Properties extends Hashtable<Object,Object>

所以是在
Hashtable.get(key)

public synchronized V get(Object key) {
       Entry<?,?> tab[] = table;   
       int hash = key.hashCode();
       //这里面的数据结构就相当于Map下有List,先将key Hash后在Map中查出hash后的List,然后遍历list
       int index = (hash & 0x7FFFFFFF) % tab.length;  //通过与运算给负数最前面的1改为0并和tab.length进行取膜
       for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
           if ((e.hash == hash) && e.key.equals(key)) {
               return (V)e.value;
           }
       }
       return null;
   }

出现未知量table?
table查找发是Hashtable本身

private transient Entry<?,?>[] table;
//那么什么时候初始化的呢?
//是在构造方法中
public Hashtable() {
        this(11, 0.75f);
    }
public Hashtable(int initialCapacity, float loadFactor) {
       ...
        table = new Entry<?,?>[initialCapacity];
        ...
    }

所以table初始为11大小的Entry<?,?>数组
这个问题完事后,就是defaults
defaults
是在Properties类下

protected Properties defaults;
//构造器中初始化为null
 public Properties() {
  this(null);
 }
  public Properties(Properties defaults) {
     this.defaults = defaults;
 }

okok现在看完了SpringProperties.getProperty(key)方法后,

总结

如果在之前将这个key,setProperty进去后,那么他就能获取到他的参数,
如果获取到为true,那么最开始的shouldIgnoreSpel属性就是为true
通过源码分析到,底层的getProperty(key)是利用hash表的存储结构, Entry<?,?>[]
初始时为11
okok Spring源码学习一到这里,主要讲了初始化了3个变量.。
下一阶段要查看Spring源码真正的方法,
有错误或者不妥的地方欢迎指正

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值