一、starter :场景启动器

SpringBoot将每个场景都抽取成一个start,通过引入starts,就能使用到相应的场景功能。即使是这样,SpringBoot也不能囊括开发中所有的场景,我们往往需要自定义starters,来简化对SpringBoot的使用。

当我们写好某一个场景的starter,其他开发人员可以直接引用我们写好的starter,而不需要进行过多的配置。

二、自定义starters

1、准备

(1)这个场景需要使用到的依赖是什么?
(2)如何编写自动配置

我们参考 WebMvcAutoConfiguration

源码:

注意:@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class }) 注解的作用:

是使加了@ConfigurationProperties(prefix = "spring.mvc") 注解生效

 

2、编写自动配置遵循的规范

其中,@ConditionalOnXXX 表示符合这个条件,就会去实例化该配置类

@Configuration //指定这个类是一个配置类
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter //指定自动配置类的顺序
@Bean //给容器中添加组件
@ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容器中自动配置类要能加载
将需要启动就加载的自动配置类,配置在META‐INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\

3、编写自动配置的模式

(1)图

(2) 分析

(3)例如 spring-boot-starter-web

四、自定义starters项目搭建

1、创建一个空项目

2、新建一个Modules : Maven工程 作为 启动器

3、新建一个SpringBoot快速启动模块 作为 自动配置类

4、修改两个项目的pom依赖文件

(1) dhu-spring-boot-starter

    <groupId>com.dhu.starter</groupId><artifactId>dhu-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version><!--启动器--><dependencies><!--引入自动配置模块,其他开发人员只需要引入我们的启动器就可以--><dependency><groupId>com.dhu.starter</groupId><artifactId>dhu-spring-boot-starter-autoconfigurer</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies>

(2)dhu-spring-boot-starter-autoconfigurer

    <groupId>com.dhu.starter</groupId><artifactId>dhu-spring-boot-starter-autoconfigurer</artifactId><version>0.0.1-SNAPSHOT</version><name>dhu-spring-boot-starter-autoconfigurer</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><!--引入spring-boot-starter;所有starter的基本配置--><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency></dependencies>

5、编写代码:

创建了一个自动配置类 HelloServiceAutoConfiguration,它会给容器中添加 HelloService 组件,

HelloService 中用到的属性跟 HelloProperties 绑定。

同时,也配置了:自动配置类要能加载,将需要启动就加载的自动配置类,配置在META‐INF/spring.factories

(1)HelloProperties

package com.dhu.starter;import org.springframework.boot.context.properties.ConfigurationProperties;/*** @author zhou* @create 2020/6/1*/
@ConfigurationProperties(prefix = "dhu.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;}
}

(2)HelloService

package com.dhu.starter;/*** @author zhou* @create 2020/6/1*/
public class HelloService {private HelloProperties helloProperties;public HelloProperties getHelloProperties() {return helloProperties;}public void setHelloProperties(HelloProperties helloProperties) {this.helloProperties = helloProperties;}public String sayHelloDhu(String name){return helloProperties.getPrefix() + "-" + name + helloProperties.getSuffix();}
}

(3)HelloServiceAutoConfiguration

package com.dhu.starter;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author zhou* @create 2020/6/1*/
@Configuration
@ConditionalOnWebApplication //web应用才生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {@Autowiredprivate HelloProperties helloProperties;@Beanpublic HelloService helloService(){HelloService service = new HelloService();service.setHelloProperties(helloProperties);return service;}
}

(4)META-INF/spring.factories

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.dhu.starter.HelloServiceAutoConfiguration

6、将这两个模块安装到Maven仓库

五、编写SpringBoot项目进行测试

1、pom.xml

       <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--引入自定义的starter--><dependency><groupId>com.dhu.starter</groupId><artifactId>dhu-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version></dependency>

2、application.properties

dhu.hello.prefix=DHU
dhu.hello.suffix=WELCOME

3、HelloController

4、运行结果

SpringBoot如何自定义starters相关推荐

  1. IDEA实现SpringBoot自定义Starters

    尝试多次,总是会有一个坑,Maven安装Starters成功,可以在External Libraries看到打包好的jar,也能打开里面源码文件,但是没法在项目中引用的情况. 最后参考https:// ...

  2. springboot banner在线生成_SpringBoot系列教程10--小花样之SpringBoot配置自定义Banner

    SpringBoot系列教程10--小花样之SpringBoot配置自定义Banner 作者:一一哥 一. Spring Boot 常用配置 本章节主要介绍一下 Spring Boot 中的一些常用配 ...

  3. springboot编写自定义过滤器

    springboot编写自定义过滤器 首先引入依赖,其次编写过滤器 @Configuration public class FilterRegisterConfig {@Beanpublic Filt ...

  4. Flowable springboot项目自定义中文字体

    Flowable springboot项目自定义中文字体 摘要:在flowable框架中,当我们想要集成springboot框架的时候,可能要设置中文字体,flowable6.4之前的版本因为没有可以 ...

  5. SpringBoot之自定义验证码

    代码地址如下: http://www.demodashi.com/demo/14280.html 项目介绍 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问 ...

  6. springboot+aop+自定义注解,打造通用的全局异常处理和参数校验切面(通用版)

    springboot+aop+自定义注解,打造通用的全局异常处理和参数校验切面(通用版) 参考文章: (1)springboot+aop+自定义注解,打造通用的全局异常处理和参数校验切面(通用版) ( ...

  7. Springboot 之 自定义配置文件及读取配置文件

    读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. 核心 ...

  8. SpringBoot2.x系列教程10--小花样之SpringBoot配置自定义Banner

    SpringBoot系列教程10--小花样之SpringBoot配置自定义Banner 作者:一一哥 一. Spring Boot 常用配置 本章节主要介绍一下 Spring Boot 中的一些常用配 ...

  9. 八、Spring Boot自定义starters

    一.自定义starters • 自动装配Bean: – 自动装配使用配置类(@Configuration)结合Spring4 提供的条件判断注解 @Conditional及Spring Boot的派生 ...

最新文章

  1. ARM 环境下使用azure powershell 从远程blob中拉去vhd 并创建虚拟机
  2. 在后SCI时代需要什么样的人才?
  3. Vue.js 实现v-if和v-else来切换CSS样式
  4. 第一章:初识lucene
  5. Python-jieba分词学习及应用
  6. 大学计算机基础python学多久_基于Python 的“大学计算机基础”课程教学设计
  7. c语言简短的泡沫排序法编程,冒泡排序--简单(c语言)
  8. oracle堆表和MySQL_聚簇索引对比|Oracle vs MySQL
  9. 苹果自动驾驶“排名垫底”,每1.1英里就发生一次脱离...
  10. Dijkstra算法python可视化实现
  11. 光学计算机的工作原理,使用光学计算机的人工智能超分辨率
  12. 计算机管理系统权限申请审批表,开通权限申请书范文
  13. 如何更改Apple Watch的方向
  14. 论文推介:CaTT-KWS—基于级联Transducer-Transformer的多阶段自定义关键词识别框架
  15. 团队springboot基础镜像选择思考
  16. java 格式化输出xml_Java格式化输出Xml
  17. 4个万兆光口+16个千兆光口+8个千兆combo光电复用口万兆三层核心工业以太网交换机HY5700-854XG16GX8GC
  18. mysql查询每个部门的最高和最低工资_SQL数据库 计算出每个部门的平均工资 最高工资和最低工资 语法怎么写?...
  19. 网站访问量统计实现.
  20. android视频流加密,android 视频 加密/解密(使用AES)

热门文章

  1. Nginx入门与应用
  2. [GXYCTF2019]BabyUpload 1 文件上传
  3. Bootstrap的css如何导入angular框架
  4. rem的正确使用姿势 -- 完美解决H5页面不同尺寸屏幕的适配问题
  5. linux命令 final,shell 命令大全先贴一下FinalShell程序的官方网
  6. 网吧计算机配置特点,为何网吧电脑配置不高,但玩起来却一点都不卡?网管说出真相!...
  7. vscode导入python包_使用Visual Studio Code将请求导入Python
  8. 我们有了新的名字——太魔人(Timers)
  9. iPhone中播放声音
  10. 数据科学作业2_房屋交易价格预测