创建自己的Spring Boot Starter
抽取通用模块作为项目的一个spring boot starter。可参照mybatis的写法。
IDEA创建Empty Project并添加如下2个module,一个基本maven模块,另一个引入spring-boot-starter依赖。
1) xxx-spring-boot-starter - 引入依赖并管理依赖版本
demo-spring-boot-starter
<dependencies>
<dependency>
<groupId>org.chris</groupId>
<artifactId>demo-spring-boot-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
2) xxx-spring-boot-autoconfigure - xxx的自动配置类
demo-spring-boot-autoconfigure
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
属性类DemoProperties
@ConfigurationProperties(prefix = "chris.demo")
public class DemoProperties {
private String name;
private String content; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
}
}
功能类DemoService
public class DemoService {
private DemoProperties demoProperties;
public DemoProperties getDemoProperties() {
return demoProperties;
}
public void setDemoProperties(DemoProperties demoProperties) {
this.demoProperties = demoProperties;
}
public String demoShow(){
return this.demoProperties.getName() + " ----- " + this.demoProperties.getContent();
}
}
自动配置类DemoAutoConfiguration
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(DemoProperties.class)
public class DemoAutoConfiguration { @Autowired
private DemoProperties demoProperties; @Bean
public DemoService demoService(){
DemoService demoService = new DemoService();
demoService.setDemoProperties(demoProperties);
return demoService;
}
}
最后添加DemoAutoConfiguration到EnableAutoConfiguration中

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.chris.springboot.DemoAutoConfiguration
在新的spring boot项目中如果需要引用以上starter,只需要在依赖中添加如下,
<dependency>
<groupId>org.chris</groupId>
<artifactId>demo-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
测试类DemoController
@RestController
public class DemoController { @Autowired
private DemoService demoService; @GetMapping("demo")
public String demo(){
return demoService.demoShow();
}
}
附上代码
创建自己的Spring Boot Starter的更多相关文章
- Spring Boot Starter 介绍
http://www.baeldung.com/spring-boot-starters 作者:baeldung 译者:http://oopsguy.com 1.概述 依赖管理是任何复杂项目的关键部分 ...
- Spring Boot (一): Spring Boot starter自定义
前些日子在公司接触了spring boot和spring cloud,有感于其大大简化了spring的配置过程,十分方便使用者快速构建项目,而且拥有丰富的starter供开发者使用.但是由于其自动化配 ...
- 自定义的Spring Boot starter如何设置自动配置注解
本文首发于个人网站: 在Spring Boot实战之定制自己的starter一文最后提到,触发Spring Boot的配置过程有两种方法: spring.factories:由Spring Boot触 ...
- 手把手教你定制标准Spring Boot starter,真的很清晰
写在前面 我们每次构建一个 Spring 应用程序时,我们都不希望从头开始实现具有「横切关注点」的内容:相反,我们希望一次性实现这些功能,并根据需要将它们包含到任何我们要构建的应用程序中 横切关注点 ...
- 年轻人的第一个自定义 Spring Boot Starter!
陆陆续续,零零散散,栈长已经写了几十篇 Spring Boot 系列文章了,其中有介绍到 Spring Boot Starters 启动器,使用的.介绍的都是第三方的 Starters ,那如何开发一 ...
- 从零开始开发一个Spring Boot Starter
一.Spring Boot Starter简介 Starter是Spring Boot中的一个非常重要的概念,Starter相当于模块,它能将模块所需的依赖整合起来并对模块内的Bean根据环境( 条件 ...
- 一个简单易上手的短信服务Spring Boot Starter
前言 短信服务在用户注册.登录.找回密码等相关操作中,可以让用户使用更加便捷,越来越多的公司都采用短信验证的方式让用户进行操作,从而提高用户的实用性. Spring Boot Starter 由于 S ...
- 最详细的自定义Spring Boot Starter开发教程
1. 前言 随着Spring的日渐臃肿,为了简化配置.开箱即用.快速集成,Spring Boot 横空出世. 目前已经成为 Java 目前最火热的框架了.平常我们用Spring Boot开发web应用 ...
- Spring Boot Starter 开发指南
Spring Boot Starter是什么? 依赖管理是任何复杂项目的关键部分.以手动的方式来实现依赖管理不太现实,你得花更多时间,同时你在项目的其他重要方面能付出的时间就会变得越少. Spring ...
随机推荐
- LCA【p4281】[AHOI2008]紧急集合 / 聚会
Description 欢乐岛上有个非常好玩的游戏,叫做"紧急集合".在岛上分散有N个等待点,有N-1条道路连接着它们,每一条道路都连接某两个等待点,且通过这些道路可以走遍所有的等 ...
- Guess Number Higher or Lower II -- LeetCode
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- Remove Duplicate Letters -- LeetCode
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- [Git]Git 常用的操作命令
创建本地仓库 git init 获取远程仓库 git clone [url] 例如:git clone https://github.com/you/yourpro.git 创建远程仓库 添加一个新的 ...
- 【Git】windows上git命令中文乱码的问题
windows上git命令中文乱码的问题解决 1.打开git bash快捷方式启动 2.右键 options 3.进入text选项卡,选中中文 和UTF-8 4.应用 测试[中文正常显示] 尝试打开文 ...
- kubernetes1.5.2集群部署过程--安全模式
使用https安全模式部署kubernetes集群,能保证集群通讯安全.有效限制非授权用户访问.但部署比非安全模式复杂的多. 本文为etcd.kubernetes集群中各个组件配置证书认证,所有组件通 ...
- Struts2实现登录权限访问控制
目录: Ⅰ 条件 Ⅱ 目的 Ⅲ 分析 Ⅳ 实现 Ⅴ 具体代码实现 ------------------------------------------------------------------- ...
- 利用PPPOE认证获取路由器中宽带账号密码
前言 回家时买了一台极路由准备换掉家里老掉牙的阿里路由器,想进后台看一下宽带账号密码,咦???后台密码是什么来着??? 我陷入了沉思,家里的路由器一般都是pppoe拨号,而路由器在与pppoe认证服务 ...
- 【AS3 Coder】任务五:Flash 2D游戏的第二春(下)
在上一节中,我们基本上已经讲完了游戏中最主要的逻辑部分,不过为了更加全面地运用Starling中的一些特性,在本节中我们将一起来看看如何实现多面板切换以及粒子效果,这两个玩意儿可是比较频繁会出现于St ...
- log4j输出日志到flume
现需要通过log4j将日志输出到flume,通过flume将日志写到文件或hdfs中 配置flume-config文件 将日志下沉至文件 a1.sources = r1 a1.sinks = k1 a ...