一、starter的作用

  当我们实现了一个组建,希望尽可能降低它的介入成本,一般的组建写好了,只要添加spring扫描路径加载spring就能发挥作用。有个更简单的方式扫描路径都不用加,直接引入jar就能使用。

  原理时因为springboot提供一个配置文件 spring.factories,预定好了加载那个配置类。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xjw.HelloAutoConfiguration

二、自定义实现starter

1、创建pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>generateCode-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>generateCode-starter</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build> </project>

2、创建配置类

package com.starter.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class GeneraterConfig { @Bean
GeneraterIdService getGeneraterIdService(){
return new GeneraterIdService();
} }

3、组建类:GeneraterIdService.java

package com.starter.demo;

import java.util.Date;

public class GeneraterIdService {
public String generaterId(){
return new Date().getTime()+"";
}
}

4、spring.factories文件

路径:resources/META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.starter.demo.GeneraterConfig

三、打包测试

1、引入

<dependency>
<groupId>com.example</groupId>
<artifactId>generateCode-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

2、使用测试

@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class RestTemplateTest {
@Autowired
GeneraterIdService generaterIdService; @Test
public void TestGeneraterId(){
System.out.println(generaterIdService.generaterId());
} }

springboot系列十四、自定义实现starter的更多相关文章

  1. SpringBoot系列(十二)过滤器配置详解

    SpringBoot(十二)过滤器详解 往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件 ...

  2. struts2官方 中文教程 系列十四:主题Theme

    介绍 当您使用一个Struts 2标签时,例如 <s:select ..../>  在您的web页面中,Struts 2框架会生成HTML,它会显示外观并控制select控件的布局.样式和 ...

  3. Springboot系列(四)web静态资源配置详解

    Springboot系列(四)web静态资源配置 往期精彩 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 SpringBoot系列(三)配 ...

  4. SpringBoot第二十四篇:应用监控之Admin

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11457867.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   前一章(S ...

  5. 学习ASP.NET Core Razor 编程系列十四——文件上传功能(二)

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  6. SpringBoot | 第二十四章:日志管理之AOP统一日志

    前言 上一章节,介绍了目前开发中常见的log4j2及logback日志框架的整合知识.在很多时候,我们在开发一个系统时,不管出于何种考虑,比如是审计要求,或者防抵赖,还是保留操作痕迹的角度,一般都会有 ...

  7. 闯祸了,生成环境执行了DDL操作《死磕MySQL系列 十四》

    由于业务随着时间不停的改变,起初的表结构设计已经满足不了如今的需求,这时你是不是想那就加字段呗!加字段也是个艺术活,接下来由本文的主人咔咔给你吹. 试想一下这个场景 事务A在执行一个非常大的查询 事务 ...

  8. MP实战系列(十四)之分页使用

    MyBatis Plus的分页,有插件式的,也有其自带了,插件需要配置,说麻烦也不是特别麻烦,不过觉得现有的MyBatis Plus足以解决,就懒得配置插件了. MyBatis Plus的资料不算是太 ...

  9. SpringBoot | 第十四章:基于Docker的简单部署

    前言 讲解了单元测试,本章节讲讲应用的部署吧.总体而言,在进行自动化部署时,基本都会用上Jenkins+Maven+Docker进行部署.本章节主要讲解使用Maven+Docker进行SpringBo ...

随机推荐

  1. 【BZOJ2426】[HAOI2010]工厂选址(贪心)

    [BZOJ2426][HAOI2010]工厂选址(贪心) 题面 BZOJ 洛谷 题解 首先看懂题目到底在做什么. 然而发现我们显然可以对于每个备选位置跑一遍费用流,然后并不够优秀. 不难发现所有的位置 ...

  2. nginx 深入篇

    nginx 防盗链 上文介绍了如何以最最简单的方式配置静态资源,还存在一定的隐患,一般的盗链如何预防, 设置验证referer server { listen 8000; server_name 12 ...

  3. HDU 4289 Control (网络流,最大流)

    HDU 4289 Control (网络流,最大流) Description You, the head of Department of Security, recently received a ...

  4. bzoj3545 Peaks

    题意:多次求从点x出发经过边权不超过k的边能走到的点中第k大的权值. 解:离线排序 + 并查集 + 线段树合并. 题面有锅...是第k大的权值不是第k大的山. #include <cstdio& ...

  5. A1053. Path of Equal Weight

    Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight of ...

  6. [luogu1351][联合权值]

    题目地址 https://www.luogu.org/problemnew/show/P1351 题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为 ...

  7. 多线程之间的通信(等待唤醒机制、Lock 及其它线程的方法)

    一.多线程之间的通信. 就是多个线程在操作同一份数据, 但是操作的方法不同. 如: 对于同一个存储块,其中有两个存储位:name   sex, 现有两个线程,一个向其中存放数据,一个打印其中的数据. ...

  8. weichat Small 程序

    微信小程序教程用什么语言  ? 答:php java html5 css, 咫尺微页很棒可以自制 html 它是一套自有的语言(暂且叫 WeLang),使用 WeLang 开发出来的页面. 一个微信页 ...

  9. linux 一些命令(2)

    1.查看centos版本 # lsb_release -a 2.查找大于1000M的文件 find / -type f -size +1000M -print0 | xargs -0 du –h

  10. 函数和常用模块【day04】:函数式编程(六)

    本节内容 1.概述 2.介绍 3.定义 一.概述 每当提到函数式编程,很多人都以为就是我们经常写的函数.其实不然. 函数:是定义一段代码的执行过程 函数式编程:输入是确定的,输出也是确定的 二.介绍 ...