SpringBoot启动原理以及过程分析

我们启动一个springboot项目,最简单的就是配置一个springboot启动类,然后运行即可

@SpringBootApplication
public class SpringBoot {
	public static void main(String[] args) {
		SpringApplication.run(SpringBoot.class, args);
	}
}

通过上面的代码,我们可以看出springboot启动的关键主要有两个地方,第一个就是@SpringBootApplication注解,第二个就是 SpringApplication.run(SpringBoot.class, args);这个方法下面我们就研究这两个地方都做了什么?

@SpringBootApplication原理解析

@SpringBootApplication上面除了元注解,有三个我们感兴趣的三个注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan

@SpringBootConfiguration

@SpringBootConfiguration的源代码,看下他由哪些注解组成

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration

可以看到,除去元注解,剩下的@Configuration注解,其实就是在@Configuration包了一层,和@Configuration一样的效果。

@ComponentScan

我们先说下@ComponentScan作用。他的作用就是扫描当前包以及子包,将有@Component,@Controller,@Service,@Repository等注解的类注册到容器中,以便调用。
注:如果@ComponentScan不指定basePackages,那么默认扫描当前包以及其子包,而@SpringBootApplication里的@ComponentScan就是默认扫描,所以我们一般都是把springboot启动类放在最外层,以便扫描所有的类。

@EnableAutoConfiguration

@EnableAutoConfiguration的工作原理,大家后面看的应该会更清晰:
它主要就是通过内部的方法,扫描classpath的META-INF/spring.factories配置文件(key-value),将其中的org.springframework.boot.autoconfigure.EnableAutoConfiguration 对应的配置项实例化并且注册到spring容器。我们同样打开@EnableAutoConfiguration源码,可以发现他是由以下几个注解组成的

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)

除去元注解,主要注解就是
@AutoConfigurationPackage和@Import(AutoConfigurationImportSelector.class)
首先我们先进入AutoConfigurationImportSelector类,可以看到他有一个方法selectImports()
在这里插入图片描述
继续跟踪,进入getAutoConfigurationEntry()方法

List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);

我们继续跟踪getCandidateConfigurations()方法!可以看到这里有个方法

SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),getBeanClassLoader());

这个方法的作用就是读取classpath下的META-INF/spring.factories文件的配置,将key为 org.springframework.boot.autoconfigure.EnableAutoConfiguration 对应的配置项读取出来,通过反射机制实例化为配置文件,然后注入spring容器。

SpringApplication.run()原理解析

我们通过源码可以看到SpringApplication.run(),有两部分如下:
第一部分:new SpringApplication(primarySources)
第二部分:run(args)方法

 public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
        return run(new Class[]{primarySource}, args);
    }

//----------------------------------------
  public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
        return (new SpringApplication(primarySources)).run(args);
    }

进入第一部分,创建一个SpringApplication对象

 public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
        this.sources = new LinkedHashSet();
        this.bannerMode = Mode.CONSOLE;
        this.logStartupInfo = true;
        this.addCommandLineProperties = true;
        this.addConversionService = true;
        this.headless = true;
        this.registerShutdownHook = true;
        this.additionalProfiles = new HashSet();
        this.isCustomEnvironment = false;
        this.resourceLoader = resourceLoader;
        Assert.notNull(primarySources, "PrimarySources must not be null");
        this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
        /**判定当前应用的类型,一共有三种类,
        REACTIVE( 响应式web应用),
        SERVLET(基于servlet的web应用),
        NONE(非web应用,即不会启动服务器)
        */
        this.webApplicationType = WebApplicationType.deduceFromClasspath();
        
		//获取所有初始化器 
		this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
		        
		//获取所有监听器     
		this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
		
		//定位main方法
        this.mainApplicationClass = this.deduceMainApplicationClass();
    }

进入getSpringFactoriesInstances()方法

	private <T> Collection<T> getSpringFactoriesInstances(Class<T> type) {
		return getSpringFactoriesInstances(type, new Class<?>[] {});
	}
	
	private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
		ClassLoader classLoader = getClassLoader();
		// Use names and ensure unique to protect against duplicates
		//获取所有初始化器的名称集合
		Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
		//根据名称集合实例化这些初始化器
		List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
		//排序		
		AnnotationAwareOrderComparator.sort(instances);
		return instances;
	}

从代码可以看出是在META-INF/spring.factories配置文件里获取初始化器,然后实例化、排序后再设置到initializers属性中。

调用run方法,启动run()

run()方法调用分析:


    public ConfigurableApplicationContext run(String... args) {
    	// 开启计时类进行计时
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        //声明应用上下文
        ConfigurableApplicationContext context = null;
        // 记录springboot启动异常日志
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
        //设置系统java.awt.headless属性,默认为true(跟踪代码可以看到)
        this.configureHeadlessProperty();
        // 获取运行监听器,它的作用是监听容器的创建,启动,运行各个过程。
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        //遍历调用监听器,表示监听器已经开始初始化容器(系统正在启动)
        listeners.starting();

        Collection exceptionReporters;
        try {
        	// 将args包装厂ApplicationArguments类(将传过来的参数包装成一个类,方便使用)
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            //监听器开始对对环境参数进行赋值
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            this.configureIgnoreBeanInfo(environment);
            //打印banner图,就是我们springboot启动时,前面几行图形
            Banner printedBanner = this.printBanner(environment);
            // 创建ioc容器对象xxxApplicationContext(根据SpringApplication,判断创建什么容器对象)
            context = this.createApplicationContext();
            // 异常采集
            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
            // 部署上下文(下面解析)准备IOC容器信息
            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            // springbootApplication生效
			// 刷新上下文(下面解析),刷新IOC容器=====创建容器中所有的组件,
			//这个也是spring的容器创建的过程
            this.refreshContext(context);
            //刷新后的方法,空方法,给用户自定义重写
            this.afterRefresh(context, applicationArguments);
            //结束计时
            stopWatch.stop();
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
            }
			//********* 使用广播和回调机制告诉监听者springboot容器已经启动化成功**********
			//启动IOC容器
            listeners.started(context);
            this.callRunners(context, applicationArguments);
        } catch (Throwable var10) {
            this.handleRunFailure(context, var10, exceptionReporters, listeners);
            throw new IllegalStateException(var10);
        }

        try {
        //********* 使用广播和回调机制告诉已经可以运行springboot了**********
        //IOC容器正在运行中
            listeners.running(context);
            return context;
        } catch (Throwable var9) {
            this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
            throw new IllegalStateException(var9);
        }
    }

部署上下文prepareContext(),准备IOC容器信息

源码分析:

private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {
		//将环境变量set到context中,设置上下文的环境
        context.setEnvironment(environment);
        //这步操作主要是对ApplicationContext进行后置处理。
        this.postProcessApplicationContext(context);
        //这个方法就是获取之前获取到的所有initializer类型的类,并进行初始化
        //前面的ApplicationContextInitializer调用initializer方法,扩展容器
        this.applyInitializers(context);
        //这个方法是通知监听器IOC容器准备完成
        listeners.contextPrepared(context);
        //打印日志
        if (this.logStartupInfo) {
            this.logStartupInfo(context.getParent() == null);
            this.logStartupProfileInfo(context);
        }
		//获取所有BeanFactory
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        //把命令行参数信息封装成一个对象注册到IOC容器中
        beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
        if (printedBanner != null) {
        	//把Banner也注册成一个单实例IOC容器中
            beanFactory.registerSingleton("springBootBanner", printedBanner);
        }

        if (beanFactory instanceof DefaultListableBeanFactory) {
            ((DefaultListableBeanFactory)beanFactory).setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
        }

        Set<Object> sources = this.getAllSources();
        Assert.notEmpty(sources, "Sources must not be empty");
        this.load(context, sources.toArray(new Object[0]));
        //通知所有listeners,IOC容器已经加载好了
        listeners.contextLoaded(context);
    }

刷新上下文refreshContext()

源码分析:

 private void refreshContext(ConfigurableApplicationContext context) {
        this.refresh(context);
        if (this.registerShutdownHook) {
            try {
                context.registerShutdownHook();
            } catch (AccessControlException var3) {
            }
        }

    }

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值