抽取通用模块作为项目的一个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();
}
}

附上代码

demo-starter, demo

创建自己的Spring Boot Starter的更多相关文章

  1. Spring Boot Starter 介绍

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

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

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

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

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

  4. 手把手教你定制标准Spring Boot starter,真的很清晰

    写在前面 我们每次构建一个 Spring 应用程序时,我们都不希望从头开始实现具有「横切关注点」的内容:相反,我们希望一次性实现这些功能,并根据需要将它们包含到任何我们要构建的应用程序中 横切关注点 ...

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

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

  6. 从零开始开发一个Spring Boot Starter

    一.Spring Boot Starter简介 Starter是Spring Boot中的一个非常重要的概念,Starter相当于模块,它能将模块所需的依赖整合起来并对模块内的Bean根据环境( 条件 ...

  7. 一个简单易上手的短信服务Spring Boot Starter

    前言 短信服务在用户注册.登录.找回密码等相关操作中,可以让用户使用更加便捷,越来越多的公司都采用短信验证的方式让用户进行操作,从而提高用户的实用性. Spring Boot Starter 由于 S ...

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

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

  9. Spring Boot Starter 开发指南

    Spring Boot Starter是什么? 依赖管理是任何复杂项目的关键部分.以手动的方式来实现依赖管理不太现实,你得花更多时间,同时你在项目的其他重要方面能付出的时间就会变得越少. Spring ...

随机推荐

  1. geant4开发平台的构建方案的讨论a

    首先比较linux和windows下的平台的不同的功能 linux通过命令进行一系列操作,对于不习惯命令的也可以用鼠标,但是就显得过于落后,因为命令的存在自然表明其先进性. linux下的geant4 ...

  2. jquery 为表单动态添加元素

    $('<input />').attr('type','hidden')            .attr('name','type')            .attr('value', ...

  3. 网络防嗅探工具SniffJoke

    网络防嗅探工具SniffJoke   在渗透测试中,通过网络嗅探,可以获取网络通信主机的各种信息.为了防止嗅探,Kali Linux提供了专用工具SniffJoke.该工具能够自动对用户的网络数据进行 ...

  4. UVA 11396 Claw Decomposition 染色

    原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  5. centos从头学习配置web服务器环境

    为了学习linux下配置web服务器环境,于是安装了vmware,准备在虚拟机里面学习web服务器的搭建! 首先是在虚拟机里安装centos,我选择的是32位的centos6.6版本,因为新版本7据说 ...

  6. 脑科学对基金经理的八个启示 z

    脑科学对基金经理的八个启示 第一,总想要更多.人类大脑是在物资奇缺过程中进化的,所以获得任何“资源”,如食物.性.金钱等,都可以让人感觉良好,大脑也会鼓励我们继续下去. 事实上,可卡因等药物就是“绑架 ...

  7. 能上架App的GooglePlay开发者账号获取流程

    googleplay 开发者账号申请流程 接到公司号召,要让我们的app走向世界,上架GooglePlay,都说天朝的Android 程序员是折翼的天使,猛然发现写了做么多年的Android,竟然不知 ...

  8. jqGrid怎么设置初始化页面时不加载数据(不向服务器请求数据)

    最近做一些表格一直用到jqGrid,今天遇到一个问题: 1.就是页面加载的时候数据不显示,点击搜索才根据请求从服务器返回并显示内容. 2.默认不从服务器请求数据(不然在开发者工具下会显示请求不到数据的 ...

  9. 机器学习&深度学习资料(转载)

    转自 飞鸟各投林 <Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboo ...

  10. 软件测试技术---Web应用软件测试

    从测试的角度看,Web应用软件的以下特点会导致Web应用软件的测试有别于其他软件的测试 1.基于无连接协议 2.内容驱动 3.开发周期短 4.演化频繁 5.安全性要求较高 6.美观性要求较高 Web应 ...