美文网首页
SpringBoot 05 ~ 自定义starter

SpringBoot 05 ~ 自定义starter

作者: 帅可儿妞 | 来源:发表于2018-11-07 02:11 被阅读3次

    相信你一定也非常喜欢SpringBoot为我们提供的各种starter,因为SpringBoot的本质就是在原有框架的基础之上为我们提供了各种自动配置和加载这些自动配置的程序而已。有幸的是各大框架也都有自己的封装好的,比如MyBatis就有;现在我们来看看这些所谓的启动器怎么制作吧ヾ(≧O≦)〃嗷~

    starter的制作

    1. 创建Maven项目shreker-web-spring-boot-starter来作为一个starter;
    2. 创建补引用任何jar的spring-boot项目shreker-web-spring-boot-autoconfigurer作为自动配置的项目;
    3. 在Maven项目shreker-web-spring-boot-starter的dependencies中引入项目shreker-web-spring-boot-autoconfigurer;
    4. 编写各种组件:
      • 编写一个类QLWebProperties来绑定配置文件中的所有配置;修饰注解:
        • @ConfigurationProperties(prefix="<config-prefix>")
      • 在项目shreker-web-spring-boot-configurer中编写一个类:QLWebConfigurer类,用来做一些配置,并引用QLWebProperties作为成员变量;修饰注解:
      • 编写一个类QLWebAutoConfiguration用来做自动配置,把QLWebProperties@Autowired到这个类中,并把Autowired进来的QLWebProperties对象设置到QLWebConfigurer对象上,并把shreker-web-spring-boot-configurer的QLWebConfigurer编写方法@Bean注入到容器中;修饰注解:
        • @Configuration
        • @ConditionalOnWebApplication
        • @EnableConfigurationProperties(QLWebProperties.class)
    5. 在Resources中创建文件:META-INF/spring.factories,在这个文件中配置:
      # Auto Configure
      org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
      QLWebAutoConfiguration # 全类名
      
    6. starter创建完成,这时候就可以在项目的其他地方引用了,当然,你需要先将这两个项目安装在仓库中,如果有精力可以提交到Maven仓库;

    相关文章

      网友评论

          本文标题:SpringBoot 05 ~ 自定义starter

          本文链接:https://www.haomeiwen.com/subject/cjfmxqtx.html