当前位置: 首页 > 后端技术 > Java

SpringBoot自定义启动器

时间:2023-04-01 16:45:04 Java

上一期说到,关于SpringBoot自动组装的原理,相信小伙伴们已经有所了解了。今天我们就来说说如何根据自动组装的原理来定制一个启动器。什么是开胃菜?玩SpringBoot这么久,几乎所有的项目依赖基本都是各种starter,那什么是starter呢?启动器是一组方便的依赖描述符。当我们使用它的时候,我们可以得到所有需要的Spring和相关技术的一站式服务,典型的比如spring-boot-starter-web,引入后自动导入所有springweb项目相关的依赖。说实话,用了很久的SpringBoot,阿建已经忘记了被每个项目都需要引入spring-core、spring-context、spring-web等所支配的恐惧哈哈。回顾SpringBoot自动组装的内容上一节我们从探索redis的自动组装流程开始,大家还记得它的流程吗?阿健带大家回顾一下:在项目启动时,Spring通过@Import注解导入AutoConfigurationImportSelector,然后在调用该类的selectImports时,从classpath下的META-INF/spring.factories文件中读取key为EnableAutoConfiguration的配置类,然后Spring会将这些类加载到Spring容器中,并将它们变成单独的bean。动手实践的过程已经整理好了,现在开始实践吧,思路其实很简单写一个配置类,把配置类放在资源文件夹下的META-INF/spring.factories中首先创建一个项目1.创建spring-boot-starter-demo项目,写入pom.xml4.0.0org.springframework.bootspring-boot-starter-parent2.3.5.RELEASEcn.zijiancodespring-boot-starter-demo1.0.0-SNAPSHOTpomUTF-8UTF-81.8spring-boot-starter-demo-starterspring-boot-starter-demo-sample2.创建子模块spring-boot-starter-demo-starter4.0.0cn.zijiancodespring-boot-starter-demo1.0.0-SNAPSHOTspring-boot-starter-demo-starter1.0.0-SNAPSHOTorg.springframework.bootspring-boot-starter3.创建测试用模块spring-boot-starter-demo-sample4.0.0cn.zijiancodespring-boot-starter-demo1.0.0-SNAPSHOTspring-boot-starter-demo-sample1.0.0-SNAPSHOTorg.springframework.bootspring-boot-startercn.zijiancodespring-boot-starter-demo-starter1.0.0-SNAPSHOT项目结构如下:spring-boot-starter-demo-starter:我们这次的主角,自定义starterspring-boot-starter-demo-sample:测试模块,用于测试starter是否有效4.编写启动程序代码编写了一个用于注入测试的处理程序publicclassDemoHandler{publicDemoHandler(){System.out.println("演示处理程序初始化!!");}}不需要加注解,因为后面要用@Bean注入写自动配置类publicclassDemoAutoConfiguration{@BeanpublicDemoHandlerdemoHandler(){returnnewDemoHandler();}}自动配置类也不需要加任何注解,因为它本质上是使用@Import导入的,当然如果不加@Configuration注解,其实会造成一个小问题。这个小问题阿健决定保密,放到下一期跟大家聊聊。很快,这两天(其实是因为还有一些内容),在resources目录下新建了一个META-INF/。spring.factories文件写入配置org.springframework.boot.autoconfigure.EnableAutoConfiguration=\cn.zijiancode.starter.config.DemoAutoConfiguration5.在测试模块中编写测试类@SpringBootApplicationpublicclassSampleApplication{publicstaticvoidmain(String[]args){SpringApplication.run(SampleApplication.class,args).close();}}6.启动项目并查看结果。演示处理程序已成功注入到自动配置类中。也就是我们的starter生效了~我知道,到这个时候,肯定会有小伙伴说:就这?阿建:确实,就是这样,哈哈,一个常用的使用方式就到这里了~使用starter做一些额外的操作在上面的例子中,我们只是通过starter注入了bean,但实际上我们可以使用这样的机制来做更多的东西,比如spring-boot-starter-data-redis在项目启动时与redis建立连接,并初始化连接池还有我们之前学习的nacos。项目启动时,将服务注册到nacos等。那么,这样的操作应该如何进行呢?其实在例子中,我们的DemoHandler在构造方法中打印了一句demohanlderinit。这是一个小主意。我们可以在初始化bean时做其他事情。当然,这种做法不是很好。因为spring中的bean是一个一个初始化的,如果我们在U??serService的构造函数中写调用RoleService的逻辑,很可能会因为RoleService没有初始化而报错。在Spring中,还有一种叫做监听器的东西,我们可以用它来做一些事情,这也是阿健最喜欢的方式。监听器必须对应一系列事件。有一个事件叫ContextRefreshedEvent,表示Spring上下文已经刷新,所有bean都已经初始化,Spring启动过程即将结束。尝试1.向DemoHandler添加一个方法publicclassDemoHandler{publicDemoHandler(){System.out.println("demohandlerinit!!");}publicvoidhello(){System.out.println("helloworldfordemostarter!");}}2。编辑监听器publicclassDemoListenerimplementsApplicationListener{@ResourceprivateDemoHandlerdemoHandler;@OverridepublicvoidonApplicationEvent(ContextRefreshedEventevent){demoHandler.hello();}}3。会听}@BeanpublicDemoListenerdemoListener(){returnnewDemoListener();}}4.使用test模块测试测试是否成功,听听有效总结上一期介绍了如何基于SpringBoot自动组装原理自定义starter,和朋友聊了下如何使用这个机制来做一些额外的东西。本期内容比较简单,希望大家有所收获。我们的下一期……结束了。上一期我说了要和大家聊聊如何查看组件的源码和扩展。我有罪。请允许我放在下一期。「磕头.png」,下次再见~gitee:https://gitee.com/lzj960515/s...更多精彩内容,敬请关注公众号:程序员阿健,阿健欢迎你的公众号~个人博客空间:https://zijiancode.cn/archive...