一、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. 慢吞吞的pip切换源
  2. 中国牡蛎碳酸钙市场需求现状调研及十四五投资风险评估报告2022-2028年版
  3. kubernetes-Deployment
  4. 计算机面试的时候写过的代码,程序员悲催瞬间:来之不易的美团面试,我尽然挂了(还原真实场景)...
  5. java canvas 动态画图_canvas前端动图如何实现
  6. ld cannot find an existing library
  7. 信息系统安全等级保护备案任务详单
  8. 防火墙双机热备A/S模式和A/A模式原理
  9. 【字符知识】SGML 类语言(Eg.HTML)字符转义
  10. STP是一个需要众力协作的协议
  11. 用R进行文本挖掘与分析--软件分词统计词频
  12. 测绘——利用ExifTool提取照片exif信息【windows环境下】(信息非常全)
  13. 第39级台阶 蓝桥杯递归 java
  14. OKR-VUCA时代目标管理利器实践分享
  15. Python (百万答题类节目)辅助工具代码(实测有效)
  16. 题目 1224: 整除的尾数
  17. 如何写系统需求分析书
  18. coreldraw的线条怎么变成圆头_如何PS包装盒平面图改为立体图
  19. 斯人若彩虹,遇上方知有——dbGet(一)
  20. C++ pair 和make_pair的用法

热门文章

  1. 经济学跨考深大计算机,2020-2021年深圳大学经济学考研择校,参考书,历年报录比,考研经验...
  2. Twelve South 为iPhone6和iPhone6 Plus发布了新的bookbook wallet 手机套
  3. Vivido添加pynq-Z2开发板
  4. 学习LocalDate对象
  5. 看陈广老师c#参考视频总结(第六篇)
  6. python生成json文件
  7. 怎样给文件夹中的文件依次编号?
  8. 求两个数之间的随机数及猜数字游戏
  9. Word VBA批量格式转换:docx转pdf、doc、rtf、txt以及反向转换
  10. 一些实用的电脑软件推荐