SpringBoot面试题-个人准备-001(SpringBoot启动流程)

面试题题目:SpringBoot启动流程

1. 基本工程环境

  1.1 一个简单的Maven工程

                

1.2 基本pom(只需要web即可)

              

1.3 一个简单的启动类

          

2.源码大致解析

 2.1 主方法(main)

 

1.创建一个Spring应用 
    new SpringApplication(primarySources).run(args)
    2.new SpringApplication
        public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
            2.1 资源加载
            this.resourceLoader = resourceLoader;
            Assert.notNull(primarySources, "PrimarySources must not be null");
            2.2 将主类放入linkedHastSet当中
            this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
            2.3 应用类型的选择
            this.webApplicationType = WebApplicationType.deduceFromClasspath();
            2.4 设置初始化(构造)器
            setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
            2.5 设置(应用)监听器 
            setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
            2.6 配置应用的主方法所在类
            this.mainApplicationClass = deduceMainApplicationClass();
        }
    3. run(args);
        public ConfigurableApplicationContext run(String... args) {
            3.1 计时器开始
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            ConfigurableApplicationContext context = null;
            Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
            3.2 配置参数
            configureHeadlessProperty();
            3.3 配置运行监听器
            SpringApplicationRunListeners listeners = getRunListeners(args);
            3.4 监听器开始工作
            listeners.starting();
            try {
                ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
                3.5准备环境--监听器和应用参数
                ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
                3.6配置忽略的Bean信息
                configureIgnoreBeanInfo(environment);
                3.7打印图案
                Banner printedBanner = printBanner(environment);
                3.8 创建应用上下文
                context = createApplicationContext();
                3.9 异常报告
                exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
                        new Class[] { ConfigurableApplicationContext.class }, context);
                3.10 准备上下文
                prepareContext(context, environment, listeners, applicationArguments, printedBanner);
                3.11 刷新上下文
                refreshContext(context);
                3.12 刷新上下文善后
                afterRefresh(context, applicationArguments);
                3.13 计时器结束
                stopWatch.stop();
                if (this.logStartupInfo) {
                    new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
                }
                3.14 启动所有监听器
                listeners.started(context);
                callRunners(context, applicationArguments);
            }
            catch (Throwable ex) {
                3.15 处理运行失败
                handleRunFailure(context, ex, exceptionReporters, listeners);
                throw new IllegalStateException(ex);
            }
    
            try {
                3.16 将应用上下文传递给每一个监听器
                listeners.running(context);
            }
            catch (Throwable ex) {
                handleRunFailure(context, ex, exceptionReporters, null);
                throw new IllegalStateException(ex);
            }
            return context;
        }

  2.2 SpringBootApplicatoin注解  

 1. 基本属性
    1.1 exclude 排除不需要自动配置的类
    1.2 excludeName 排除不需要自动配置的类的类名
    1.3 scanBasePackages 扫描基本包(被指定的包的子包也会包扫描到)
    1.4 scanBasePackageClasses 扫描类所在包下的所有组件
 2. 注解
    2.1 SpringBootConfiguration 表明这个类提供一个Spring Boot Application(Indicates that a class provides Spring Boot   application)
    2.2 EnableAutoConfiguration 允许自动配置(将根据所导入的jar进行自动配置)
    2.3 ComponentScan 扫描哪些包或者类所在包的组件 (Repeatable 允许重复配置)

    

参考文章:https://blog.csdn.net/hfmbook/article/details/100507083

有任何问题可以联系:swd_wjw@163.com

部分问题可能理解有误或笔误

  • 9
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值