实现自定义的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. SpringBoot Redis 订阅发布

    一  配置application.yml spring: redis: jedis: pool: max-active: 10 min-idle: 5 max-idle: 10 max-wait: 2 ...

  2. 【JZOJ6293】迷宫

    description analysis 有没有想起[\(NOIP2018\)]保卫王国? 设\(tr[t][x][y]\)表示线段树上的\(t\)节点代表的区间,从最左边列的\(x\)行到最右边列\ ...

  3. 异或+桶——cf768C

    有个结论是到最后肯定出现循环节..感觉这种做法有点歪 正解当然是题解啦 虽然到了1e8,但是cf上还是能过的 #include<bits/stdc++.h> #define rep(i,s ...

  4. LUOGU P4042 [AHOI2014/JSOI2014]骑士游戏 (spfa+dp)

    传送门 解题思路 首先设\(f[x]\)表示消灭\(x\)的最小花费,那么转移方程就是 \(f[x]=min(f[x],\sum f[son[x]] +s[x])\),如果这个转移是一个有向无环图,那 ...

  5. 牛客多校第六场 J Upgrading Technology dp

    题意: 有n个技能,一开始都是0级,第i个技能从j-1级升到j级,花费$c_{i,j}$,但是花费不一定是正的 所有的技能升到j级时,奖励$d_j$但是奖励也不一定是正的 题解: 用sum[i][j] ...

  6. ssm下使用分页插件PageHelper进行分页

    1. 导入maven依赖: <dependency> <groupId>com.github.pagehelper</groupId> <artifactId ...

  7. 修改 ulimit 参数

    sudo vim /etc/security/limits.conf 文件下加: * hard nofile 999999 * soft nofile 999999 * soft nproc 1024 ...

  8. javaSpring学习总结day_02

    使用注解注入: 1.用于创建bean对象 @Component: 作用:相当于配置了一个bean标签 位置:类上面 属性:value,含义是bean的id,当不写时,有默认值,默认值是当前类的短名,首 ...

  9. 初识OpenCV-Python - 007: 平滑图像

    本节内容主要将如何平滑图像.如通过低通道滤波模糊图像.或者自定义滤波处理图像. import cv2import numpy as npfrom matplotlib import pyplot as ...

  10. POJ 3304 /// 判断线段与直线是否相交

    题目大意: 询问给定n条线段 是否存在一条直线使得所有线段在直线上的投影存在公共点 这个问题可以转化为 是否存在一条直线与所有的线段同时相交 而枚举直线的问题 因为若存在符合要求的直线 那么必存在穿过 ...