实现自定义的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. RabbitMQ学习第二记:工作队列的两种分发方式,轮询分发(Round-robin)和 公平分发(Fair dispatch)

    1.什么是RabbitMQ工作队列 我们在应用程序使用消息系统时,一般情况下生产者往队列里插入数据时速度是比较快的,但是消费者消费数据往往涉及到一些业务逻辑处理导致速度跟不上生产者生产数据.因此如果一 ...

  2. Bootstrap 附加导航(Affix)插件

    附加导航(Affix)插件允许指定 <div> 固定在页面的某个位置.一个常见的例子是社交图标.它们将在某个位置开始,但当页面点击某个标记,该 <div> 会锁定在某个位置,不 ...

  3. Java 多线程 - 原子操作AtomicInteger & CAS(Compare-and-Swap)

    原子类简介:https://www.cnblogs.com/stephen0923/p/4505902.html AtomicInteger 介绍: https://yuwenlin.iteye.co ...

  4. Vim统计字符串出现次数

    关键命令: :%s/pattern//gn 参数说明: % - 指明操作区间,%表示全文本:可以使用1,$或者行区间代替 s – substitute,表示替换 pattern - 要查找的字符串 / ...

  5. 1003CSP-S模拟测试赛后总结

    我是垃圾……我只会骗分. 拿到题目通读一遍,感觉T3(暴力)是个树剖+线段树. 刚学了树刨我这个兴奋啊.然而手懒决定最后再说. 对着T1一顿yyxjb码了个60pts的测试点分治就失去梦想了.(顺便围 ...

  6. MySQL高可用(Galera Cluster)

    Galera Cluster简介 Galera Cluster是集成了Galera插件的MySQL集群,是一种新型的,数据不共享的,高度冗余的高可用方案,目前Galera Cluster有两个版本,分 ...

  7. BZOJ 4557 (JLOI 2016) 侦查守卫

    4557: [JLoi2016]侦察守卫 Time Limit: 20 Sec Memory Limit: 256 MB Submit: 493 Solved: 342 [Submit][Status ...

  8. iOS开发之SceneKit框架--实战地月系统围绕太阳旋转

    1.创建地月太阳系统scn文件 注意:moon在earth结构下,earth和moon在sun结构下. 2.获取scn中模型的对应节点和自转(太阳为例) 获取节点: name是对应的Identity字 ...

  9. idea运行tomcat,控制台中文乱码

    加入参数:-Dfile.encoding=UTF-8

  10. iOS逆向系列-theos

    概述 theos是GitHub开源的一个项目,通过nic.pl创建tweak项目.通过编写我们注入代码,然后执行编译.打包.安装等操作将代码注入iPhone安装的制定程序. theos环境配置 安装签 ...