面试官:能手写一个SpringBoot的Start吗?

start出现历史:

 

    SpringBoot的相对于SpringMVC的出现初衷就是无须繁琐的配置文件就可以轻松使用自己需要的功能, 我们都知道SpringBoot你只需要引入对应的XXX-spring-boot-start. 然后写上我们的配置属性文件就可以直接使用对应的增强功能。

 

    众所周知我们都知道直接引入Start和配置相关properties文件就可以使用我们的功能,那么springBoot底层又是如何集成解决自动配置的呢~

 

springBoot-start原理:

@SpringBootApplication 点击进去查看源码。

 

@EnableAutoConfiguration这个注解,我们从英文字面上理解就是开启自动配置, 我们再点击这个注解进入源码分析

 

 

进来后我们可以看到一个@Import的class 我们点击进入查看相关源码。

我们可以看到一个重要的方法 selectImports方法,我们阅读代码查看到调了this.getAutoConfigurationEntry(autoConfigurationMetadata, annotationMetadata);

这个方法. 再跟踪getAutoConfigurationEntry源码

 

 

我们可以看到它调用了 getCandidateConfiguration这个方法,顾名思义就是根据调解导入相关的自动配置。这里我们就大概明白了,为什么只有引入了依赖才会使用对应的start功能。

 

点击this.getCandidateConfigurations方法查看实现

 

 

 

我们看到它的底层调用了 SpringFactorysLoader.loadFactoryNames()这个方法。

 

再次点击进去查看源码

 

 

看到关键的一句话,就是 

classLoader.getResources("META-INF/spring.factories"),这里我们就明白为什么我们手写一个Start要在META-INF 下新建一个spring.factories的文件了,因为它启动的时候会去读取这个配置文件。

 

这里小编也遇到了一个面试题,首页问我如何写一个start。我答上来了,可是说到为什么要写一个这样的文件,我也知道它启动会去加载,但是没仔细看源码就回答的不是很好。特意记录一下

 

至此我们就大概说完了启动加载过程,我们再看如何读取这些配置文件的

 

我们可以看到springBoot自带了 spring-boot-autoconfigure这个依赖包,正是这个包自动帮助我们来处理的,

 

 

查看spring的启动配置 它有自己的spring.factories 我们可以看到 它有个

Auto Configure 的注解地方,  有一段配置

 

org.springframework.boot.autoconfigure.EnableAutoConfiguration=

 

至此我们应该明白 我们为什么也要在 spring.factories中写一个这样的属性指向我们对应的XXX-AutoConfiguration

 

我们再看看springBoot默认自己有的AutoConfiguration

 

 

还有JMS等等 如下图

 

 

我们可以看到ActiveMQAutoConfiguration 对应再配置文件有指定对应的路径,我们点这个配置类 ActiveMQAutoConfiguration 

 

 

我们就可以看到它是如何读取我们的activeMQ属性文件中的值的。我们查看ActiveMQProperties这个类

 

 

看到此次我们似乎就明白了,只需要引入Start和配置相关的配置文件,SpringBoot就能帮助我们自动注入对应的bean提供我们直接使用了

 

总结:

    如果手写Start我们就可以分为大致三步了

    1.新建META-INF/spring.factories的文件 里面的XXX-AutoConfig指向我们的AutoConfig

    2.新建XXX-AutoConfig 在文件中实例化我们需要的Bean交给Spring管理

    3.新建属性文件 properties 

 

编码不易,你们的关注是我永远坚持的动力, 感谢再看或分享的小伙伴们.

 学习交流群

 

 

当然可以!以下是一个简单的示例,展示了如何手写一个Spring Boot Starter: 首先,创建一个 Maven 项目,并添加以下依赖项: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.5.4</version> </dependency> </dependencies> ``` 接下来,创建一个自定义的自动配置类,用于配置你的 Starter: ```java @Configuration @EnableConfigurationProperties(MyStarterProperties.class) public class MyStarterAutoConfiguration { private final MyStarterProperties properties; public MyStarterAutoConfiguration(MyStarterProperties properties) { this.properties = properties; } // 在此处定义你的自动配置逻辑 @Bean public MyStarterService myStarterService() { return new MyStarterService(properties); } } ``` 然后,创建一个属性类,用于将外部配置绑定到属性上: ```java @ConfigurationProperties("my.starter") public class MyStarterProperties { private String message; // 提供 getter 和 setter } ``` 最后,创建一个自定义的服务类,该服务类将在你的 Starter 中使用: ```java public class MyStarterService { private final MyStarterProperties properties; public MyStarterService(MyStarterProperties properties) { this.properties = properties; } public void showMessage() { System.out.println(properties.getMessage()); } } ``` 现在,你的 Spring Boot Starter 已经准备就绪了!你可以将其打包并使用在其他 Spring Boot 项目中。在其他项目的 `pom.xml` 文件中,添加你的 Starter 依赖: ```xml <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>my-starter</artifactId> <version>1.0.0</version> </dependency> </dependencies> ``` 然后,在你的应用程序中使用 `MyStarterService`: ```java @SpringBootApplication public class MyApplication implements CommandLineRunner { private final MyStarterService myStarterService; public MyApplication(MyStarterService myStarterService) { this.myStarterService = myStarterService; } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Override public void run(String... args) throws Exception { myStarterService.showMessage(); } } ``` 这样,你就成功地创建了一个简单的 Spring Boot Starter!当其他项目引入你的 Starter 并运行时,将会输出预定义的消息。 当然,这只是一个简单的示例,真实的 Starter 可能包含更多的配置和功能。你可以根据自己的需求进行扩展和定制。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你微笑莳很美丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值