自定义Starter


是什么

starter可以理解是一组封装好的依赖包,包含需要的组件和组件所需的依赖包,使得使用者不需要再关注组件的依赖问题

所以一个staerter包含

  1. 提供一个autoconfigure类
  2. 提供autoconfigure类的依赖
怎么做

创建starter大概需要

  1. 需要一个配置类bean,来填充配置
  2. 获取配置信息,注册到容器
  3. 将配置类加到自动配置

导入自动装配和Spring boot依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-autoconfigure</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter</artifactId>
  8. </dependency>

创建一个bean

  1. @Data
  2. public class HelloworldService {
  3. private String words;
  4. public String sayHello() {
  5. return "hello, " + words;
  6. }
  7. }

创建peoperties类来自定义需要的参数,用来接收properties或者yml里的配置

  1. //helloword就是配置的前缀
  2. @ConfigurationProperties(prefix = "helloword")
  3. public class HelloworldProperties {
  4. public static final String DEFAULT_WORDS = "world";
  5. //这个值就是helloword.words
  6. private String words = DEFAULT_WORDS;
  7. public String getWords() {
  8. return words;
  9. }
  10. public void setWords(String words) {
  11. this.words = words;
  12. }
  13. }

创建configureation类来注册bean

将配置的参数注入到bean里,在注册到容器

  1. @Configuration
  2. //指定条件成立的情况下自动配置类生效(往哪装配)
  3. @ConditionalOnClass(HelloworldService.class)
  4. //让xxxProperties生效加入到容器中
  5. @EnableConfigurationProperties(HelloworldProperties.class)
  6. public class HelloworldAutoConfiguration {
  7. // 注入属性类
  8. @Autowired
  9. private HelloworldProperties hellowordProperties;
  10. @Bean
  11. // 当容器没有这个 Bean 的时候才创建这个 Bean
  12. @ConditionalOnMissingBean(HelloworldService.class)
  13. public HelloworldService helloworldService() {
  14. HelloworldService helloworldService = new HelloworldService();
  15. helloworldService.setWords(hellowordProperties.getWords());
  16. return helloworldService;
  17. }
  18. }

在启动类里标明需要自动装配

  1. @EnableAutoConfiguration//开启自动装配
  2. @ComponentScan({"test"})
  3. public class TestApplication {
  4. }

最后是指定装配哪个配置类

在resources目录下创建META-INF文件夹

然后创建spring.factories标注需要装配的配置类

  1. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  2. test.config.HelloworldAutoConfiguration

最后就可以用这个starter了

首先导入这个starter

  1. <dependency>
  2. <groupId>org.example</groupId>
  3. <artifactId>test-spring-boot-starter</artifactId>
  4. <version>1.0-SNAPSHOT</version>
  5. </dependency>

然后导入HelloworldService这个bean

和在yml里配置参数

  1. helloword:
  2. words : hello

就完成了

SprinigBoot自定义Starter的更多相关文章

  1. SpringBoot之旅第六篇-启动原理及自定义starter

    一.引言 SpringBoot的一大优势就是Starter,由于SpringBoot有很多开箱即用的Starter依赖,使得我们开发变得简单,我们不需要过多的关注框架的配置. 在日常开发中,我们也会自 ...

  2. Spring Boot 自定义 starter

    一.简介 SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入springboot 为我提供的这些场景启动器,我们再进行少量的配置就能使用相应 ...

  3. java框架之SpringBoot(10)-启动流程及自定义starter

    启动流程 直接从 SpringBoot 程序入口的 run 方法看起: public static ConfigurableApplicationContext run(Object source, ...

  4. SpringBoot应用篇(一):自定义starter

    一.码前必备知识 1.SpringBoot starter机制 SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在mave ...

  5. SpringBoot第十六篇:自定义starter

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11058502.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言   这一段时间 ...

  6. 小代学Spring Boot之自定义Starter

    想要获取更多文章可以访问我的博客 - 代码无止境. 上一篇小代同学在Spring Boot项目中配置了数据源,但是通常来讲我们访问数据库都会通过一个ORM框架,很少会直接使用JDBC来执行数据库操作的 ...

  7. (springboot)自定义Starter

    要引入的jar项目,即自定义的Starter项目: pom:(这里不能引入springboot整合否则测试项目注入失败) <?xml version="1.0" encodi ...

  8. SpringBoot自定义starter及自动配置

    SpringBoot的核心就是自动配置,而支持自动配置的是一个个starter项目.除了官方已有的starter,用户自己也可以根据规则自定义自己的starter项目. 自定义starter条件 自动 ...

  9. 对照谈-官方spring-boot-starter和自定义starter异同分析

    在前面我讲用spring-boot-starter-mail发邮件的时候,我侧重看的是spring boot发邮件的便利性,今天,我们聊下另外一个方面,spring-boot-starter自身的结构 ...

随机推荐

  1. Go xmas2020 学习笔记 05、Arrays, Slices, and Maps

    05-Arrays, Slices, and Maps. In memory. Array. Slice. fence post error. Compare Array and Slice . Ma ...

  2. toFixed()与银行家舍入

    toFixed()与银行家舍入 一直在用toFixed()方法做浮点数的舍入取值,如果只是客户端展示数据是没有多大问题的,但是如果涉及到和后端互交,数据的精度可能会导致接口对接失败,当然了,涉及安全性 ...

  3. formData一般用法,移动端,pc端都可以用,pc有兼容性问题

    其实FormData是一个 对象他是一个比较新的东东(其实我也不知道改叫什么好) 利用FormData对象,你可以使用一系列的键值对来模拟一个完整的表单,然后使用XMLHttpRequest发送这个& ...

  4. HMS Core Discovery第14期直播预告~纵享丝滑剪辑,释放视频创作力

    [导读] 拍摄.导入.特效.卡点.BGM-几步简单的操作,我们便可将生活的瞬间用视频记录与分享.应用前沿AI技术,提供一站式视频处理能力,帮助开发者们构建更智能.更易用.更专业的视频剪辑软件,打造视频 ...

  5. 在django中使用orm来操作MySQL数据库的建表,增删改

    多张表之间的三种关系:一对一,一对多,多对多 创建表 一对一 xx = models.OneToOneField(to='表明',to_field='字段名',on_delete=models.CAS ...

  6. Bootstrap Blazor 模板使用(一)Layout 组件

    原文链接:https://www.cnblogs.com/ysmc/p/16197223.html BootstrapBlazor 官网地址:https://www.blazor.zone Boots ...

  7. XCTF练习题---MISC---something_in_image

    XCTF练习题---MISC---something_in_image flag:Flag{yc4pl0fvjs2k1t7T} 解题步骤: 1.观察题目,下载附件,这是一道2019湖湘杯的题目 2.下 ...

  8. k8s入门之PV和PVC(八)

    某些应用是要持久化数据的,如果直接持久化在pod所在节点,当pod因为某种原因重建或被调度另外一台节点,是无法访问到之前持久化的数据,所以需要一个公共存储数据的地方,无论pod删除重建或重新调度到其他 ...

  9. pyqt5 重启相同线程错误:QThread: Destroyed while thread is still running

    背景: 把一个基于QObject的类的槽运行在另一个线程,我们可以用moveToThread的方法. 1 新建一个子线程类,编写槽函数和信号,MyClass *m_MyClass=new MyClas ...

  10. 【openstack】cloudkitty组件,入门级安装(快速)

    @ 目录 前言 架构 安装 配置 启动 检索并安装 CloudKitty 的仪表板 前言 什么是CloudKitty? CloudKitty是OpenStack等的评级即服务项目.该项目旨在成为云的退 ...