springboot系列十四、自定义实现starter
一、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的更多相关文章
- SpringBoot系列(十二)过滤器配置详解
SpringBoot(十二)过滤器详解 往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件 ...
- struts2官方 中文教程 系列十四:主题Theme
介绍 当您使用一个Struts 2标签时,例如 <s:select ..../> 在您的web页面中,Struts 2框架会生成HTML,它会显示外观并控制select控件的布局.样式和 ...
- Springboot系列(四)web静态资源配置详解
Springboot系列(四)web静态资源配置 往期精彩 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 SpringBoot系列(三)配 ...
- SpringBoot第二十四篇:应用监控之Admin
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11457867.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 前一章(S ...
- 学习ASP.NET Core Razor 编程系列十四——文件上传功能(二)
学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...
- SpringBoot | 第二十四章:日志管理之AOP统一日志
前言 上一章节,介绍了目前开发中常见的log4j2及logback日志框架的整合知识.在很多时候,我们在开发一个系统时,不管出于何种考虑,比如是审计要求,或者防抵赖,还是保留操作痕迹的角度,一般都会有 ...
- 闯祸了,生成环境执行了DDL操作《死磕MySQL系列 十四》
由于业务随着时间不停的改变,起初的表结构设计已经满足不了如今的需求,这时你是不是想那就加字段呗!加字段也是个艺术活,接下来由本文的主人咔咔给你吹. 试想一下这个场景 事务A在执行一个非常大的查询 事务 ...
- MP实战系列(十四)之分页使用
MyBatis Plus的分页,有插件式的,也有其自带了,插件需要配置,说麻烦也不是特别麻烦,不过觉得现有的MyBatis Plus足以解决,就懒得配置插件了. MyBatis Plus的资料不算是太 ...
- SpringBoot | 第十四章:基于Docker的简单部署
前言 讲解了单元测试,本章节讲讲应用的部署吧.总体而言,在进行自动化部署时,基本都会用上Jenkins+Maven+Docker进行部署.本章节主要讲解使用Maven+Docker进行SpringBo ...
随机推荐
- 【BZOJ3821/UOJ46】玄学(二进制分组,线段树)
[BZOJ3821/UOJ46]玄学(二进制分组,线段树) 题面 BZOJ UOJ 题解 呜,很好的题目啊QwQ. 离线做法大概可以线段树分治,或者直接点记录左右两次操作时的结果,两个除一下就可以直接 ...
- luogu2312 [NOIp2015]解方程 (秦九韶)
秦九韶算法:多项式$a_0+a_1x+a_2x^2+...+a_nx^n=a_0+x(a_1+x(a_2+...+(xa_n))..)$,这样对于一个x,可以在O(n)求出结果 为了避免高精度,我们同 ...
- 在Android中通过导入静态数据库来提高应用第一次的启动速度
一个Android应用给用户的第一印象非常重要,除了要有好的创意和美观的界面,性能也是很关键的部分,本文讨论的就是第一次启动的速度问题. Android应用的启动过程不能让用户等待太长时间,个人觉得最 ...
- Centos6.5之ssh免密码登录配置
Centos6.5之ssh免密码登录配置 centos ssh 免密码登录 0.说明 这里为了方便说明问题,假设有A和B两台安装了centos6.5的主机.目标是实现A.B两台主机分别能够通过ssh免 ...
- A1086. Tree Traversals Again
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...
- 读入字符串/字符 scanf与getchar/gets区别
1. 读入字符 scanf/getchar:空格.Tab.回车都可以读入.但要以回车作为结束符. 所以当读入字符时,注意去掉一些干扰输入的字符,如空格和回车 2. 读入字符串 scanf:不能读入空格 ...
- 编译RocketMQ
目前最新版本为 官方没有提供可执行的安装包,只提供了maven项目,去GitHub下载 点击Download ZIP 就可以了,如果你安装了版本控制工具 SVN 或者 GIT 就可以使用它上边的 UR ...
- Qt Designer 的使用
1. Qt Designer 快速入门 Qt Designer 是交互式可视化GUI设计工具,可以帮助我们快速开发 PyQt 程序的速度. 它生成的 UI 界面是一个后缀为 .ui 的文件,可以通过 ...
- 编写前程贷投标loadrunner脚本及总结
1.完成前程贷的(登录,投标) 2.所有的返回信息都用关联函数(web_reg_save_param_ex)进行关联 3.对返回信息用(strcmp)函数进行if判断 4.总结(web_reg_sav ...
- IntelliJ IDEA和Eclipse最常用的快捷键对应表:
描述 Eclipse Intellij 代码补全 Ctrl+space Ctrl+space 打开类或者接口 (两个IDE都支持使用“驼峰字符”前缀的方式来过滤查找列表,进而轻松完成搜索:比如:可 以 ...