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

Springboot底层启动过程是怎样的

2024/3/24 23:31:26发布13次查看
springapplication构造分析1、记录 beandefinition 源
spring容器刚开始是空的,要去各个源找到beandefinition,这些源可能是配置类,可能是xml文件。在构造方法里会获取一个主源,也就是引导类,根据引导类去获取beandefinition。
2、推断应用类型
根据jar包去判断是什么引用类型
3、记录 applicationcontext 初始化器
对applicationcontext做扩展
4、记录监听器
监听重要事件
5、推断主启动类
记录运行的主类。
springapplication run分析1、得到 springapplicationrunlisteners,名字取得不好,实际是事件发布器
发布 application starting 事件,在程序启动的重要节点发布事件
public static void main(string[] args) throws exception{ // 添加 app 监听器 springapplication app = new springapplication(); app.addlisteners(e -> system.out.println(e.getclass())); // 获取事件发送器实现类名 list<string> names = springfactoriesloader.loadfactorynames(springapplicationrunlistener.class, a39_2.class.getclassloader()); for (string name : names) { system.out.println(name); class<?> clazz = class.forname(name); constructor<?> constructor = clazz.getconstructor(springapplication.class, string[].class); springapplicationrunlistener publisher = (springapplicationrunlistener) constructor.newinstance(app, args); // 发布事件 defaultbootstrapcontext bootstrapcontext = new defaultbootstrapcontext(); publisher.starting(bootstrapcontext); // spring boot 开始启动 publisher.environmentprepared(bootstrapcontext, new standardenvironment()); // 环境信息准备完毕 genericapplicationcontext context = new genericapplicationcontext(); publisher.contextprepared(context); // 在 spring 容器创建,并调用初始化器之后,发送此事件 publisher.contextloaded(context); // 所有 bean definition 加载完毕 context.refresh(); publisher.started(context); // spring 容器初始化完成(refresh 方法调用完毕) publisher.running(context); // spring boot 启动完毕 publisher.failed(context, new exception("出错了")); // spring boot 启动出错 }
2、封装启动 args
3、准备 environment 添加命令行参数(*)
public static void main(string[] args) throws ioexception { applicationenvironment env = new applicationenvironment(); // 系统环境变量, properties, yaml env.getpropertysources().addlast(new resourcepropertysource(new classpathresource("step3.properties"))); env.getpropertysources().addfirst(new simplecommandlinepropertysource(args)); for (propertysource<?> ps : env.getpropertysources()) { system.out.println(ps); }// system.out.println(env.getproperty("java_home")); system.out.println(env.getproperty("server.port")); }
4、configurationpropertysources 处理(*)
发布 application environment 已准备事件
public static void main(string[] args) throws ioexception, nosuchfieldexception { applicationenvironment env = new applicationenvironment(); env.getpropertysources().addlast( new resourcepropertysource("step4", new classpathresource("step4.properties")) ); configurationpropertysources.attach(env); for (propertysource<?> ps : env.getpropertysources()) { system.out.println(ps); } system.out.println(env.getproperty("user.first-name")); system.out.println(env.getproperty("user.middle-name")); system.out.println(env.getproperty("user.last-name")); }}
5、通过 environmentpostprocessorapplicationlistener 进行 env 后处理(*)
application.properties,由 standardconfigdatalocationresolver 解析
spring.application.json
public class step5 { public static void main(string[] args) { springapplication app = new springapplication(); app.addlisteners(new environmentpostprocessorapplicationlistener()); /*list<string> names = springfactoriesloader.loadfactorynames(environmentpostprocessor.class, step5.class.getclassloader()); for (string name : names) { system.out.println(name); }*/ eventpublishingrunlistener publisher = new eventpublishingrunlistener(app, args); applicationenvironment env = new applicationenvironment(); system.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增强前"); for (propertysource<?> ps : env.getpropertysources()) { system.out.println(ps); } publisher.environmentprepared(new defaultbootstrapcontext(), env); system.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增强后"); for (propertysource<?> ps : env.getpropertysources()) { system.out.println(ps); } } private static void test1() { springapplication app = new springapplication(); applicationenvironment env = new applicationenvironment(); system.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增强前"); for (propertysource<?> ps : env.getpropertysources()) { system.out.println(ps); } configdataenvironmentpostprocessor postprocessor1 = new configdataenvironmentpostprocessor(new deferredlogs(), new defaultbootstrapcontext()); postprocessor1.postprocessenvironment(env, app); system.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增强后"); for (propertysource<?> ps : env.getpropertysources()) { system.out.println(ps); } randomvaluepropertysourceenvironmentpostprocessor postprocessor2 = new randomvaluepropertysourceenvironmentpostprocessor(new deferredlog()); postprocessor2.postprocessenvironment(env, app); system.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增强后"); for (propertysource<?> ps : env.getpropertysources()) { system.out.println(ps); } system.out.println(env.getproperty("server.port")); system.out.println(env.getproperty("random.int")); system.out.println(env.getproperty("random.int")); system.out.println(env.getproperty("random.int")); system.out.println(env.getproperty("random.uuid")); system.out.println(env.getproperty("random.uuid")); system.out.println(env.getproperty("random.uuid")); }}
6、绑定 spring.main 到 springapplication 对象(*)
把配置文件中的值赋给springapplication的默认属性值
public class step6 { // 绑定 spring.main 前缀的 key value 至 springapplication, 请通过 debug 查看 public static void main(string[] args) throws ioexception { springapplication application = new springapplication(); applicationenvironment env = new applicationenvironment(); env.getpropertysources().addlast(new resourcepropertysource("step6", new classpathresource("step6.properties"))); system.out.println(application); binder.get(env).bind("spring.main", bindable.ofinstance(application)); system.out.println(application); }
7、打印 banner(*)
public class step7 { public static void main(string[] args) { applicationenvironment env = new applicationenvironment(); springapplicationbannerprinter printer = new springapplicationbannerprinter( new defaultresourceloader(), new springbootbanner() ); // 测试文字 banner// env.getpropertysources().addlast(new mappropertysource("custom", map.of("spring.banner.location","banner1.txt"))); // 测试图片 banner// env.getpropertysources().addlast(new mappropertysource("custom", map.of("spring.banner.image.location","banner2.png"))); // 版本号的获取 system.out.println(springbootversion.getversion()); printer.print(env, step7.class, system.out); }}
8、创建容器
private static genericapplicationcontext createapplicationcontext(webapplicationtype type) { genericapplicationcontext context = null; switch (type) { case servlet -> context = new annotationconfigservletwebserverapplicationcontext(); case reactive -> context = new annotationconfigreactivewebserverapplicationcontext(); case none -> context = new annotationconfigapplicationcontext(); } return context; }
9、准备容器发布
application context 已初始化事件
10、加载 bean 定义
发布 application prepared 事件
defaultlistablebeanfactory beanfactory = context.getdefaultlistablebeanfactory(); annotatedbeandefinitionreader reader1 = new annotatedbeandefinitionreader(beanfactory); xmlbeandefinitionreader reader2 = new xmlbeandefinitionreader(beanfactory); classpathbeandefinitionscanner scanner = new classpathbeandefinitionscanner(beanfactory); reader1.register(config.class); reader2.loadbeandefinitions(new classpathresource("b03.xml")); scanner.scan("com.itheima.a39.sub");
11、refresh 容器
发布 application started 事件
12、执行 runner
发布 application ready 事件
这其中有异常,发布 application failed 事件
以上就是springboot底层启动过程是怎样的的详细内容。
该用户其它信息

VIP推荐

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