Spring Boot Starter自定义实现三步曲
实现自定义的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自定义实现三步曲的更多相关文章
- Spring Boot (一): Spring Boot starter自定义
前些日子在公司接触了spring boot和spring cloud,有感于其大大简化了spring的配置过程,十分方便使用者快速构建项目,而且拥有丰富的starter供开发者使用.但是由于其自动化配 ...
- 年轻人的第一个自定义 Spring Boot Starter!
陆陆续续,零零散散,栈长已经写了几十篇 Spring Boot 系列文章了,其中有介绍到 Spring Boot Starters 启动器,使用的.介绍的都是第三方的 Starters ,那如何开发一 ...
- 自定义spring boot starter 初尝试
自定义简单spring boot starter 步骤 从几篇博客中了解了如何自定义starter,大概分为以下几个步骤: 1 引入相关依赖: 2 生成属性配置类: 3 生成核心服务类: 4 生成自动 ...
- 自定义的Spring Boot starter如何设置自动配置注解
本文首发于个人网站: 在Spring Boot实战之定制自己的starter一文最后提到,触发Spring Boot的配置过程有两种方法: spring.factories:由Spring Boot触 ...
- 最详细的自定义Spring Boot Starter开发教程
1. 前言 随着Spring的日渐臃肿,为了简化配置.开箱即用.快速集成,Spring Boot 横空出世. 目前已经成为 Java 目前最火热的框架了.平常我们用Spring Boot开发web应用 ...
- Membership三步曲之入门篇 - Membership基础示例
Membership 三步曲之入门篇 - Membership基础示例 Membership三步曲之入门篇 - Membership基础示例 Membership三步曲之进阶篇 - 深入剖析Pro ...
- [转]Membership三步曲之入门篇 - Membership基础示例
本文转自:http://www.cnblogs.com/jesse2013/p/membership.html Membership三步曲之入门篇 - Membership基础示例 Members ...
- Spring Boot Starter 介绍
http://www.baeldung.com/spring-boot-starters 作者:baeldung 译者:http://oopsguy.com 1.概述 依赖管理是任何复杂项目的关键部分 ...
- Java Spring Boot VS .NetCore (三)Ioc容器处理
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
随机推荐
- ARM GNU 常用汇编伪指令介绍
abort .abort: 停止汇编 .align absexpr1, absexpr2: 以某种对齐方式,在未使用的存储区域填充值. 第一个值表示对齐方式,4, 8,16 或 32. 第 二个表 ...
- ld.so和ld-linux.so* :动态链接器/加载器(转)
概述 动态链接器可以被正在运行的动态链接程序或者动态对象(没有对动态链接器指定命令选项,动态链接器被存储在程序的.interp区域)间接调用,也可以直接运行程序, 例如:/lib/ld-linux.s ...
- Java 集群高可用监控(结合阿里SLB)脚本
欢迎点评,大家一起来优化 计划思路: 只有在mysql slave java 进程状态都正常的情况下才允许nginx 运行, 否则就干掉它, 负载用的是阿里的SLB #bin/bash #邮件函数 ...
- C++ 字符串相互转换 适合 lua project
#include <iostream> #include <Windows.h> #include <assert.h> #define Main main voi ...
- (转)Java ClassLoader详解
转:http://java.chinaitlab.com/base/804400.html 类加载器是 Java 语言的一个创新,也是 Java 语言流行的重要原因之一.它使得 Java 类可以被动态 ...
- JAVA 文件的上传下载
一.上传文件 1.使用 transferTo 上传 @ResponseBody @RequestMapping(value = "/file/upload") public Res ...
- 分类算法之朴素贝叶斯分类(Naive Bayesian classification)
分类算法之朴素贝叶斯分类(Naive Bayesian classification) 0.写在前面的话 我个人一直很喜欢算法一类的东西,在我看来算法是人类智慧的精华,其中蕴含着无与伦比的美感.而每次 ...
- H2数据库的基本使用
文章目录 下载jar包 启动服务 下载jar包 下载h2-1.3.176.jar 这个包(部分服务版本不一致,请自行更换版本) 启动服务 从终端定位到刚才jar包下载的位置,比如我这里是Downloa ...
- idea for mac 项目打开的情况下import project
commad + ,进入设置页面,搜索框输入menu,选择file下面的任意一个都行: 点击打开的窗口中的others 不需要重启 参考:https://blog.csdn.net/zengxiaos ...
- css3 ---2 属性的选择器
存在和值属性选择器1:[attr]:该选择器选择包含 attr 属性的所有元素,不论 attr 的值为何. [name]{ background: pink; } <!DOCTYPE html& ...