1.自定义启动器

lmh-hello-spring-boot-starter(启动器)
lmh-hello-spring-boot-starter-autoconfigure(自动配置包)
1.1 创建一个空项目

1.2 在空项目的基础上,添加maven空项目,项目名称叫做
lmh-hello-spring-boot-starter
再添加一个springboot空项目,项目名称lmh-hello-spring-boot-starter-autoconfigure

1.3 lmh-hello-spring-boot-starter(启动器)的pom.xml文件中,将lmh-hello-spring-boot-starter-autoconfigure的groupId和artifactId作为依赖添加进去

1.4 注意每个类所在路径
创建HelloService类,业务处理的过程

public class HelloService {@AutowiredHelloProperties helloProperties;/** param:用户名* decription:再用户名前后 加入前后缀* */public String sayHello(String userName){return helloProperties.getPrefix()+":"+userName+" : "+helloProperties.getSuffix();}}

创建HelloProperties类

@ConfigurationProperties("lmh.hello")  //可用于便捷配置属性值
public class HelloProperties {private String prefix;private String suffix;public String getPrefix() {return prefix;}public void setPrefix(String prefix) {this.prefix = prefix;}public String getSuffix() {return suffix;}public void setSuffix(String suffix) {this.suffix = suffix;}
}

创建HelloServiceAutoConfiguration类

@Configuration
@ConditionalOnMissingBean(HelloService.class) //当容器中没有HelloService组件时,下面才生效
@EnableConfigurationProperties(HelloProperties.class)  // 默认放在容器中
public class HelloServiceAutoConfiguration {@Beanpublic HelloService helloService(){HelloService helloService = new HelloService();return helloService;}}

创建spring.factories

# Auto configure  启动时,就开始加载 HelloServiceAutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.auto.HelloServiceAutoConfiguration

1.5 将启动器和配置包 先clean,再install添加到maven仓库中![在这里
1.6 使用
在新项目中,添加启动器的依赖

<dependency><groupId>org.example</groupId><artifactId>lmh-hello-spring-boot0-starter</artifactId><version>1.0-SNAPSHOT</version>
</dependency>

application.properties中

lmh.hello.prefix=username
lmh.hello.suffix=77777

创建HelloController类

@RestController
public class HelloController {@AutowiredHelloService helloService;@GetMapping("/hello")public String sayHello(){String s = helloService.sayHello("李白");return s;}
}

1.7 地址访问
localhost:8080/hello

2.starter启动原理

  1. pom.xml 添加starter,引入autoConfigure包

starter—> autoConfigure—>spring-boot-starter

  1. autoconfigure包中配置使用META-INF/spring.factories 中EnableAutoConfiguration的值,使项目启动加载指定的自动配置类

  2. 编写自动配置类 xxxAutoConfiguration --> xxxProperties

    • @ConfigurationProperties(“lmh.hello”)
    • @Configuration
    • @ConditionalOnMissingBean(HelloService.class) //当容器中没有HelloService组件时,下面才生效
    • @EnableConfigurationProperties(HelloProperties.class) // 默认放在容器中
    • @Bean 组件

springboot如何自定义starter相关推荐

  1. SpringBoot - 开发自定义starter

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

  2. springboot 简单自定义starter - beetl

    使用idea新建springboot项目beetl-spring-boot-starter 首先添加pom依赖 packaging要设置为jar不能设置为pom<packaging>jar ...

  3. springboot框架学习 - 自定义 starter

    本篇主要演示 springboot 中自定义 starter 的实例,不涉及底层原理. 首先,创建一个什么都没有的工程作为父工程,这个工程不需要进行任何操作,然后创建两个模块,分别命名为 tyu-he ...

  4. SpringBoot自定义Starter(自动配置类)

    前言 SpringBoot其实从诞生以来围绕的核心就是快速构建项目,快速构建的前提是有人帮你做好轮子,开发者只要拿来即用就好了,而造好轮子的人就是SpringBoot的开发者,引入自动配置的形式帮助开 ...

  5. @configurationproperties注解的使用_徒手使用SpringBoot自定义Starter启动器

    前言 在使用SpringBoot框架时,SpringBoot 最强大的功能就是把我们常用的场景抽取封装成了一个个starter,将其称之为场景启动器.搭建项目时,通过引入SpringBoot为我提供的 ...

  6. 玩转 SpringBoot 2.x 之自定义Starter依赖

    前言 SpringBoot 核心功能之一就是 starter 依赖也叫起步依赖,他默认提供了 spring-boot-starter-web.spring-boot-starter-logging.s ...

  7. 根据自动装配原理在Springboot项目中自定义starter,并实现热插拔技术,自定义@enable

    根据自动装配原理在Springboot项目中自定义starter,并实现热插拔技术 自定义starter 简单步骤 使用测试 优化(热插拔技术) 自定义starter 简单步骤 创建项目,并引入aut ...

  8. SpringBoot 自定义Starter(阿里云短信、消息推送)

    首先在IDEA中创建SpringBoot项目,引入相关必要依赖,本次以阿里云短信/消息推送为例: <dependency><groupId>com.aliyun</gro ...

  9. spring boot @value取不到值_看看大厂如何自定义starter,还真想不到

    我自己整理了一套java架构资料,还有大厂面试题,需要私信回复"资料"就好了,无偿分享,希望对你有帮助. 背景 使用过Spring Boot的小伙伴都应该知道,一个Spring B ...

  10. 进阶篇-SpringBoot2.x自定义starter启动器

    1.本篇前言 Spring Boot为我们提供了简化企业级开发绝大多数场景的 starter pom[比如springb-boot-starter-web,springb-boot-starter-j ...

最新文章

  1. 哪个更快:Java 堆还是本地内存
  2. NYOJ_5739最大岛屿(河南省第八届acm程序设计大赛)
  3. cesium 3dtiles 加载本地数据_记一次Cesium地形数据生成过程
  4. python压缩文件不带根路径_python 压缩文件(解决压缩路径问题)
  5. java 级联删除文件夹下的所有文件
  6. pycharm断点调试django
  7. Linux系统下搭建BUG管理系统---禅道
  8. VC++6.0中内存泄漏检测 转
  9. shell自定义数组元素分隔符
  10. Rust —— 一门没有GC的语言
  11. 安装MapGIS IGServer遇到的问题
  12. 海思isp图像处理芯片_最新海思芯片3559A的功能简介
  13. Go的研习笔记-day12(以Java的视角学习Go)
  14. android highcharts柱状图实例,Highcharts 基本柱形图
  15. Javascript的交互性
  16. SDKMAN 安装软件失败
  17. 三行python代码实现人工智能
  18. POJ1753 Flip Game
  19. python爬虫公众号_python爬虫_微信公众号推送信息爬取的实例
  20. Matlab:License checkout failed. License Manager Error-95解决办法与Matlab加速启动

热门文章

  1. BokTalk块说全球首款基于区块链技术的即时通讯系统
  2. 视频与编解码的技术邂逅,碰撞出的高清罗曼史
  3. 2019 中兴秋招笔试题1
  4. 网络口碑理论的精髓:AISAS
  5. 一、HTML基础(笔记)
  6. 解决Ubuntu下博通网卡驱动问题
  7. PYTHON实战:从百度资讯爬取信息作为正文,巨潮资讯网爬取有关PDF作为附件,自动发送邮件(Mysql做存储)
  8. 绘制logo软件-AI(illustrator)
  9. cewl工具(URL字典生成器)
  10. 小弟需要一个能显示播放音频的各频率音量大小(不知道叫什么东东)的控件~!...