您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
三六零分类信息网 > 贵港分类信息网,免费分类信息发布

SpringBoot启动原理是什么

2025/10/5 10:38:54发布42次查看
入口版本: 2.1.8.release
启动代码:
@springbootapplcationpublic static void main(string[] args) { springapplication.run(blogadminapplication.class, args); system.out.println("======== admin start success... ==========");}
这里传入了两个参数,blogadminapplication当前类和args参数
我们点击进入run方法查看
public static configurableapplicationcontext run(class<?> primarysource, string... args) { return run(new class[]{primarysource}, args);}
这里是将我们写的启动类传入到了class[]数组中,这步就是个单纯的参数转换。
探讨primarysource参数那么问题是primarysource能接受的类型是啥样的,是不是什么类都可以接受,带着这个疑问,我们做一个测试,把这个参数给换成一个别的类呢,managercontroller类是一个我写的接口类
@springbootapplicationpublic class blogprojectapplication { public static void main(string[] args) { springapplication.run(managercontroller.class, args); system.out.println("======== admin start success... =========="); }}
控制台打印
org.springframework.context.applicationcontextexception: unable to start web server; nested exception is org.springframework.context.applicationcontextexception: unable to start servletwebserverapplicationcontext due to missing servletwebserverfactory bean.
提示不能启动服务,提示缺少了servletwebserverfactory bean 点进这个类看下
@functionalinterfacepublic interface servletwebserverfactory { webserver getwebserver(servletcontextinitializer... initializers);}
他被functionalinterface标注了,是一个函数式接口,只有一个getwebserver方法,用来获取webserver的 看下他的实现类,
这不就是提示我们缺少启动的服务容器么,说的直白点,我的理解就是他缺少可以运行的容器,我们知道,没有使用springboot项目之前,我们的项目都是跑在tomcat容器上的,当然也有使用jetty容器的。再者,我们知道springboot是对tomcat进行了内置。而springboot不仅仅是只有内置了tomcat,而且还内置了好多的东西,比如我们经常使用的mq、redis等等一系列的东西,这个我们可以在spring.factories配置文件中看到,这个文件位于如下位置
大概内容有下,篇幅有限,就不一一列举了。
省略。。。org.springframework.boot.autoconfigure.enableautoconfiguration=\org.springframework.boot.autoconfigure.admin.springapplicationadminjmxautoconfiguration,\org.springframework.boot.autoconfigure.aop.aopautoconfiguration,\org.springframework.boot.autoconfigure.amqp.rabbitautoconfiguration,\org.springframework.boot.autoconfigure.batch.batchautoconfiguration,\org.springframework.boot.autoconfigure.cache.cacheautoconfiguration,\省略。。。
那么回过头来,我们再看下这个问题,这些个类是如何被加载进来的,我们知道spingboot有个注解是开启自动注解的@enableautoconfiguration,他就干这个事情的。他能够激活springboot内建和自定义组件的自动装配特性。
那么,知道了这些,我们把这个之前修改后的类给改造一下,加上注解@enableautoconfiguration,看下执行效果。
@restcontroller@requestmapping("project/manager")@enableautoconfigurationpublic class managercontroller extends abstractcontroller {
运行如下
从打印信息就能知道,服务器有了,只不过下面报错,提示找不到bean,那这不就简单了么,他是不是就是没有扫描到我们的包么,这里就其实可以在配置扫描包的注解继续测试,我就懒的不测试了,直接去看@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})})public @interface springbootapplication {
@springbootapplication这个注解其实就是一个组合注解,里面包含了
元注解:用来标注他是一个注解的,jdk自带的
@springbootconfiguration:集成自@configuration,表示是一个配置类
@enableautoconfiguration:激活springboot内建和自定义组件的自动装配特性
@componentscan:扫描注解,添加了排除参数,指定排除了一些类
通过这里的实验,我们可以得出结论:
primarysource参数能接收的类是一个配置类,同时要把符合扫描规则的类装配到spring容器中,并且对springboot内置的一些类进行自动扫描到,而这里的@springbootapplication注解就是把这些特性都整合到了一起,作为了一个引导类而已。那么说白了,primarysource他接受的其实就是一个配置类。
关于注解详细知识的话,这里就聊这么多了,后面再详细聊。
args参数args是java命令行参数,我们在dos中执行java程序的时候使用“java 文件名 args参数”。args这个数组可以接收到这些参数。这个是个基础常识了。
以下我们将继续跟踪源码进行分析
我们继续追run()方法
public static configurableapplicationcontext run(class&lt;?&gt;[] primarysources, string[] args) { return (new springapplication(primarysources)).run(args);}
这个方法干了两个事情:
1、new springapplication()来创建对象
2、通过创建后的对象,调用对象里面的run()方法
以下我们将从这两个地方进行分析,本篇就先研究第一个
创建对象我们先看下他是怎么创建对象的,创建了哪些对象,
public springapplication(class<?>... primarysources) { this((resourceloader)null, primarysources);}public springapplication(resourceloader resourceloader, class<?>... primarysources) { //资源加载器 this.resourceloader = resourceloader; //断言 assert.notnull(primarysources, "primarysources must not be null"); //对primarysources进行存储到linkedhashset this.primarysources = new linkedhashset<>(arrays.aslist(primarysources)); //1、推断web应用类别 this.webapplicationtype = webapplicationtype.deducefromclasspath(); //2、加载spring应用上下文初始化 setinitializers((collection) getspringfactoriesinstances(applicationcontextinitializer.class)); //3、加载spring应用事件监听器 setlisteners((collection) getspringfactoriesinstances(applicationlistener.class)); //4、推断应用引导类 this.mainapplicationclass = deducemainapplicationclass();}
下面研究下主要流程部分
1、推断web应用类别推断web应用类型属于springboot应用web类型的初始化过程。而该类型也可在springapplication构造后,run方法执行之前,通过setwebapplicationtype(webapplicationtype webapplicationtype)方法进行调整。
在推断web应用类型的过程中,由于当前spring应用上下文尚未准备(可在代码执行顺序中看到),所以实现采用的是检查检查当前classloader下基准class的存在性判断。
上源码
this.webapplicationtype = webapplicationtype.deducefromclasspath();private static final string[] servlet_indicator_classes = { "javax.servlet.servlet", "org.springframework.web.context.configurablewebapplicationcontext" };private static final string webmvc_indicator_class = "org.springframework." + "web.servlet.dispatcherservlet";private static final string webflux_indicator_class = "org." + "springframework.web.reactive.dispatcherhandler";private static final string jersey_indicator_class = "org.glassfish.jersey.servlet.servletcontainer";private static final string servlet_application_context_class = "org.springframework.web.context.webapplicationcontext";private static final string reactive_application_context_class = "org.springframework.boot.web.reactive.context.reactivewebapplicationcontext";static webapplicationtype deducefromclasspath() {//当dispatcherhandler存在,且dispatcherservlet、servletcontainer两个不存在时;换言之,springboot仅依赖webflux存在时,此时的应用类型为reactive if (classutils.ispresent(webflux_indicator_class, null) && !classutils.ispresent(webmvc_indicator_class, null) && !classutils.ispresent(jersey_indicator_class, null)) { return webapplicationtype.reactive; } //当servlet和configurablewebapplicationcontext均不存在时,当前应用为非web应用,即webapplicationtype.none,因为这些api均是spring web mvc必须的依赖 for (string classname : servlet_indicator_classes) { if (!classutils.ispresent(classname, null)) { return webapplicationtype.none; } } //当webflux和spring web mvc同时存在时,web应用类型同样是servlet web,即webapplicationtype.servlet return webapplicationtype.servlet;}
2、加载spring应用上下文初始化setinitializers((collection) getspringfactoriesinstances(applicationcontextinitializer.class));
此过程包含两个动作,依次为getspringfactoriesinstances(applicationcontextinitializer.class)和setinitializers方法。 先看第一个过程
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 //加载了meta-inf/spring.factories资源中配置的applicationcontextinitializer实现类名单。 set<string> names = new linkedhashset<>(springfactoriesloader.loadfactorynames(type, classloader)); //初始化 list<t> instances = createspringfactoriesinstances(type, parametertypes, classloader, args, names); annotationawareordercomparator.sort(instances); return instances;}
此处使用了spring工厂加载机制方法springfactoriesloader.loadfactorynames(type, classloader)。加载了meta-inf/spring.factories资源中配置的applicationcontextinitializer实现类名单。
加载完成后使用createspringfactoriesinstances方法对其进行初始化。
private <t> list<t> createspringfactoriesinstances(class<t> type, class<?>[] parametertypes, classloader classloader, object[] args, set<string> names) { list<t> instances = new arraylist<>(names.size()); for (string name : names) { try { //从类加载器中获取指定类 class<?> instanceclass = classutils.forname(name, classloader); //判断instanceclass是不是type的子类 assert.isassignable(type, instanceclass); //根据以上获取的类名创建类的实例 constructor<?> constructor = instanceclass.getdeclaredconstructor(parametertypes); //排序 t instance = (t) beanutils.instantiateclass(constructor, args); instances.add(instance); } catch (throwable ex) { throw new illegalargumentexception("cannot instantiate " + type + " : " + name, ex); } } return instances;}
3、加载spring应用事件监听器setlisteners((collection) getspringfactoriesinstances(applicationlistener.class));
此过程与2、加载上下文初始化基本类似。 只不过初始化的对象类型变成了applicationlistener.class,setlisteners方法也只是赋值而已
public void setlisteners(collection<? extends applicationlistener<?>> listeners) { this.listeners = new arraylist<>(); this.listeners.addall(listeners);}
4、推断应用引导类this.mainapplicationclass = deducemainapplicationclass(); private class<?> deducemainapplicationclass() { try { stacktraceelement[] stacktrace = new runtimeexception().getstacktrace(); for (stacktraceelement stacktraceelement : stacktrace) { if ("main".equals(stacktraceelement.getmethodname())) { return class.forname(stacktraceelement.getclassname()); } } } catch (classnotfoundexception ex) { // swallow and continue } return null;}
这里使用到了new runtimeexception().getstacktrace()来获取堆栈信息,找到调用执行的main方法,从而确定他的类。
这里有个疑问:他不是传了primarysources数组,里面包含了类名么,怎么还用堆栈的方式去获取,此外,这里的堆栈获取也只能获取一个调用的主main方法,他为啥还要传一个class数组呢?
具体咋获取的,可以追下源码,一直跟踪他的父类throwable,找到如下代码
/** * fills in the execution stack trace. this method records within this * {@code throwable} object information about the current state of * the stack frames for the current thread. * * <p>if the stack trace of this {@code throwable} {@linkplain * throwable#throwable(string, throwable, boolean, boolean) is not * writable}, calling this method has no effect. * * @return a reference to this {@code throwable} instance. * @see java.lang.throwable#printstacktrace() */public synchronized throwable fillinstacktrace() { if (stacktrace != null || backtrace != null /* out of protocol state */ ) { fillinstacktrace(0); stacktrace = unassigned_stack; } return this;}private native throwable fillinstacktrace(int dummy);
这里最后调用了native本地方法,去爬取线程堆栈信息,为运行时栈做一份快照。
通过这个图片,可以看到整个方法的调用链,从下往上看哦
以上就是springboot启动原理是什么的详细内容。
贵港分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product