手写简易版的spring-boot-starter

墨蓝 2022-10-31 13:37 249阅读 0赞

手写简易版的spring-boot-starter

    • 第一步引入相应的jar包
    • 第二步写相应的代码
    • 第三步配置spring.factories
    • 第四步 install jar包
    • 第五步进行验证

第一步引入相应的jar包

  1. <!--引入springboot的parant包-->
  2. <parent>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-parent</artifactId>
  5. <version>2.3.0.RELEASE</version>
  6. </parent>
  7. <!---引入springboot 配置的包->
  8. <dependencies>
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-autoconfigure</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-configuration-processor</artifactId>
  16. </dependency>
  17. </dependencies>

第二步写相应的代码

  1. // 先提供一个接口
  2. public interface IService {
  3. void welcom();
  4. }
  5. // 编写接口的实现类
  6. public class IServiceImpl implements IService {
  7. @Autowired
  8. private HaochenProperties haochenProperties;
  9. @Override
  10. public void welcom() {
  11. System.out.println(haochenProperties.getName()+": 欢迎来到我的世界");
  12. }
  13. }
  14. // 提供配置类
  15. @Configuration
  16. @ConditionalOnClass(IService.class)
  17. @EnableConfigurationProperties(HaochenProperties.class)
  18. public class HaoChenAutoConfiguration {
  19. public IService iService(){
  20. return new IServiceImpl();
  21. }
  22. }
  23. // 提供配置类的属性
  24. @Configuration
  25. @ConditionalOnClass(IService.class)
  26. @EnableConfigurationProperties(HaochenProperties.class)
  27. public class HaoChenAutoConfiguration {
  28. public IService iService(){
  29. return new IServiceImpl();
  30. }
  31. }

第三步配置spring.factories

在resource下新增META-INF 并新增spring.factories文件

  1. // Auto Configure
  2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.haochen.HaoChenAutoConfiguration

第四步 install jar包

  1. mvn -Dmaven.test.skip -U clean install

第五步进行验证

导入自定义的jar包
在这里插入图片描述
在这里插入图片描述
到这就成功了。

发表评论

表情:
评论列表 (有 0 条评论,249人围观)

还没有评论,来说两句吧...

相关阅读

    相关 简易Mybatis

    手写简易的Mybatis -------------------- 此篇文章用来记录今天花个五个小时写出来的简易版mybatis,主要实现了基于注解方式的增删查改,...