SpringBoot entry (E) ----- start classes run methods did (on)

Previous say the startup class notes @SpringBootApplication general effect , this article focuses on the notes start to do something about, if necessary, click the hyperlink.

We create spingboot project, we will have a startup class, which has a line of code like this:
SpringApplication.run(DemoApplication.class,args)

He calls the static method SpringApplication the run. Let's take a brief look at what it has done
we simply followed at the source, the following is a flow chart:
Here Insert Picture Description
look at it a new SpringApplication, call the constructor have parameters, the constructor as follows:

    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.lazyInitialization = false;
		// 设置一些资源加载器
        this.resourceLoader = resourceLoader;
		// 加载类资源不能为空
        Assert.notNull(primarySources, "PrimarySources must not be null");
		//  做一个数据结构的转换
        this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
		//  判断当前项目类型是什么
        this.webApplicationType = WebApplicationType.deduceFromClasspath();
		//  设置初始化器
        this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
		//  设置监听器,springfactories 想要寻找的话,就是通过spring.factory
        this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
		// 根据class 推断应用的入口类  通过run方法调用main方法
        this.mainApplicationClass = this.deduceMainApplicationClass();
    }

There are two main ways:
WebApplicationType.deduceFromClasspath()
this.getSpringFactoriesInstances(ApplicationContextInitializer.class)

Here's a brief look at the role of these two methods:

WebApplicationType.deduceFromClasspath():

Source:

    static WebApplicationType deduceFromClasspath() {
        if (ClassUtils.isPresent("org.springframework.web.reactive.DispatcherHandler", (ClassLoader)null) && !ClassUtils.isPresent("org.springframework.web.servlet.DispatcherServlet", (ClassLoader)null) && !ClassUtils.isPresent("org.glassfish.jersey.servlet.ServletContainer", (ClassLoader)null)) {
            return REACTIVE;
        } else {
            String[] var0 = SERVLET_INDICATOR_CLASSES;
            int var1 = var0.length;

            for(int var2 = 0; var2 < var1; ++var2) {
                String className = var0[var2];
                if (!ClassUtils.isPresent(className, (ClassLoader)null)) {
                    return NONE;
                }
            }

            return SERVLET;
        }
    }

By the code we can see, this method is to judge our project in the end is what type (web engineering, basic engineering and other spring)

getSpringFactoriesInstances(ApplicationContextInitializer.class)

This way we need to look inside with, the flowchart
Here Insert Picture Description
through follow-up the following code to see the familiarMETA-INF/spring.factories, That the spring is at this timeEnableAutoConfigurationNotes are registered into the configuration bean container springioc the
first section of the new objects do things on roughly finished, talk about the next run method.

There are 10 kinds of people in the world, one is to understand binary, one is do not understand binary.

Thank you for watching, if not where to write your message, thank you.

Published 71 original articles · won praise 54 · views 420 000 +

Guess you like

Origin blog.csdn.net/weixin_43326401/article/details/104077179