实现自定义的spring boot starter,只需要三步:

1、一个Bean

2、一个自动配置类

3、一个META-INF/spring.factories配置文件

下面用代码演示这三步。

项目准备:

1、如果想使用Spring官网的脚手架自动生成项目代码,访问https://start.spring.io/

2、Maven依赖

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

代码演示:

1、一个Bean

@Data
public class HelloService {
private String msg; public String sayHello() {
return "Hello " + msg;
}
}

另外增加了一个属性配置类

@Data
@ConfigurationProperties(prefix = "hello")
public class HelloServiceProperties {
/**
* 打招呼的内容,默认为"World!"
*/
private String msg = "World!";
}

2、一个自动配置类

@Configuration
@EnableConfigurationProperties(value = HelloServiceProperties.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "hello", value = "enable", matchIfMissing = true)
public class HelloAutoConfiguration {
@Autowired
private HelloServiceProperties helloServiceProperties; @Bean
@ConditionalOnMissingBean(HelloService.class)
public HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setMsg(helloServiceProperties.getMsg()); return helloService;
}
}

3、一个META-INF/spring.factories配置文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.zyong.config.HelloAutoConfiguration

另外教一个小技巧,自动生成spring-configuration-metadata.json,这样在别的项目引用这个spring boot starter的时候,在application.properties编辑属性的时候会有提示功能,牛逼哄哄:

IntelliJ IDEA中File -> Settings -> 搜索框输入Annotation Processors -> 勾选Enable annotation processing

编译生成spring boot starter:mvn clean install

经过上面步骤,自定义的spring boot starter诞生了,接下来就可以在别的项目引用这个spring boot starter了,示例如下:

Maven依赖:

<dependency>
<groupId>com.zyong</groupId>
<artifactId>hello-spring-boot-starter</artifactId>
<version>1.0</version>
</dependency>

spring boot starter中bean的使用:

@RestController
@SpringBootApplication
public class HelloSpringBootStarterTestApplication { @Autowired
private HelloService helloService; @RequestMapping("/")
public String index() {
return helloService.sayHello();
} public static void main(String[] args) {
SpringApplication.run(HelloSpringBootStarterTestApplication.class, args);
}
}

application.properties中配置属性:

hello.msg=测试数据starter

是不是非常的简单?!YES,就是这么简单

Spring Boot Starter自定义实现三步曲的更多相关文章

  1. Spring Boot (一): Spring Boot starter自定义

    前些日子在公司接触了spring boot和spring cloud,有感于其大大简化了spring的配置过程,十分方便使用者快速构建项目,而且拥有丰富的starter供开发者使用.但是由于其自动化配 ...

  2. 年轻人的第一个自定义 Spring Boot Starter!

    陆陆续续,零零散散,栈长已经写了几十篇 Spring Boot 系列文章了,其中有介绍到 Spring Boot Starters 启动器,使用的.介绍的都是第三方的 Starters ,那如何开发一 ...

  3. 自定义spring boot starter 初尝试

    自定义简单spring boot starter 步骤 从几篇博客中了解了如何自定义starter,大概分为以下几个步骤: 1 引入相关依赖: 2 生成属性配置类: 3 生成核心服务类: 4 生成自动 ...

  4. 自定义的Spring Boot starter如何设置自动配置注解

    本文首发于个人网站: 在Spring Boot实战之定制自己的starter一文最后提到,触发Spring Boot的配置过程有两种方法: spring.factories:由Spring Boot触 ...

  5. 最详细的自定义Spring Boot Starter开发教程

    1. 前言 随着Spring的日渐臃肿,为了简化配置.开箱即用.快速集成,Spring Boot 横空出世. 目前已经成为 Java 目前最火热的框架了.平常我们用Spring Boot开发web应用 ...

  6. Membership三步曲之入门篇 - Membership基础示例

    Membership 三步曲之入门篇 - Membership基础示例 Membership三步曲之入门篇 -  Membership基础示例 Membership三步曲之进阶篇 -  深入剖析Pro ...

  7. [转]Membership三步曲之入门篇 - Membership基础示例

    本文转自:http://www.cnblogs.com/jesse2013/p/membership.html Membership三步曲之入门篇 - Membership基础示例   Members ...

  8. Spring Boot Starter 介绍

    http://www.baeldung.com/spring-boot-starters 作者:baeldung 译者:http://oopsguy.com 1.概述 依赖管理是任何复杂项目的关键部分 ...

  9. Java Spring Boot VS .NetCore (三)Ioc容器处理

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

随机推荐

  1. Apache Forbidden 403错误提示

    在配置Linux的 Apache服务时,经常会遇到http403错误,我今天配置测试时也出现了,最后解决了,总结了一下.http 403错误是拒绝访问的意思,有很多原因的.还有,这些问题在win平台的 ...

  2. 9_山寨系统调用 SystemCallEntry

    思想: 再次在 GDT 中偷内存 搭建 系统调用需要的 逻辑框架与功能实现: 基本分解妄想: 构建系统调用的代码: 拷贝到 偷取的内存中: idt 向量 序号21位置: 8003ee00`0008f1 ...

  3. 笔记23 搭建Spring MVC

    搭建一个最简单的SpringMVC示例 1.配置DispatcherServlet DispatcherServlet是Spring MVC的核心.在这里请求会第一次 接触到框架,它要负责将请求路由到 ...

  4. yii2中使用定义在 params.php文件中的配置

    yii2 使用 配置文件中在 params 的配置, 可以用 Yii::$app->params['key1']形式访问 参考 yii can't access Yii::$app->pa ...

  5. CSIC_716_20191113【装饰器进阶以及迭代器】

    装饰器的进阶主要包含叠加装饰器和有参装饰器 叠加装饰器:在一个被装饰的对象中,添加多个装饰器. 为什么要用叠加装饰器的原因:    -每一个新的功能都应该写一个新的装饰器,否则会导致,代码冗余,结构不 ...

  6. What is the difference between HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR

    What is the difference between HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR? it is impossible to say. Dif ...

  7. 树莓派3b+ 实现视频监控

    设备:树莓派3B+.Raspberry Pi Camera sudo raspi-config #启动camera sudo reboot #监测摄像头是否安装成功 raspistill -o ima ...

  8. Linux 添加时间

    添加在指令后面 `date +%Y%m%d%H%M`注意date和+之间一定要有空格 ps: %%    一个文字的 %  %a    当前locale 的星期名缩写(例如: 日,代表星期日)  %A ...

  9. scala中Map集合的简单使用

    import scala.collection.mutable /** * Map集合的简单使用 */ object MapUse { def main(args: Array[String]): U ...

  10. 蒙特卡罗定位(Particle Filter Localization)

    1. 蒙特卡罗定位 定位:机器人知道地图信息的情况下如何利用传感器信息确定自己的位置(Localization). 有人会说,定位是不需要地图信息的.机器人知道初始位置,知道左右轮的速度,就可以算出在 ...