--为啥要封装starter

--如何封装

--测试

为啥要封装starter

springboot的starter开箱即用,只需要引入依赖,就可以帮你自动装配bean,这样可以让开发者不需要过多的关注框架的配置。

如何封装

新建SpringBoot项目,引入以下依赖包到pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

ps: 官方的starter命名规范为:spring-boot-starter-{name}

非官方的starter命名规范为:{name}-spring-boot-starter

编写Service类


/**
* 字符串拼接类
*/
public class WrapService extends WrapServiceProperties{ private String str1; public WrapService(String str1){
this.str1 = str1;
} public String wrap(String newWord){
return newWord + str1;
}
}

编写配置文件读取类

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("example.config")
public class WrapServiceProperties { private String str1; public String getStr1() {
return str1;
} public void setStr1(String str1) {
this.str1 = str1;
}
}

这里的“example.config”指的是application.xml或application.yml中配置的属性的前缀,WrapServiceProperties类里面的属性会拼接到前缀上,即是完整的配置,例如这里的配置就是 example.config.str1 = xxx

编写自动配置类

import com.tjm.service.WrapService;
import com.tjm.service.WrapServiceProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
@ConditionalOnClass(WrapService.class)
@EnableConfigurationProperties(WrapServiceProperties.class)
public class AutoConfigure { @Autowired
private WrapServiceProperties properties; @Bean
@ConditionalOnMissingBean(WrapService.class)
@ConditionalOnProperty(prefix = "example.show", value="enabled", havingValue = "true")
WrapService starterService () {
return new WrapService(properties.getStr1());
}
}

对以上用到的注解解释下:

1. @ConditionalOnClass,当classpath下发现该类的情况下进行自动配置。
2. @ConditionalOnMissingBean,当Spring Context中不存在该Bean时。
3. @ConditionalOnProperty(prefix = "example.show",value = "enabled",havingValue = "true"),当配置文件中example.show.enabled=true时。

以下为SpringBoot中所有@Conditional注解和作用

@ConditionalOnBean:当容器中有指定的Bean的条件下
@ConditionalOnClass:当类路径下有指定的类的条件下
@ConditionalOnExpression:基于SpEL表达式作为判断条件
@ConditionalOnJava:基于JVM版本作为判断条件
@ConditionalOnJndi:在JNDI存在的条件下查找指定的位置
@ConditionalOnMissingBean:当容器中没有指定Bean的情况下
@ConditionalOnMissingClass:当类路径下没有指定的类的条件下
@ConditionalOnNotWebApplication:当前项目不是Web项目的条件下
@ConditionalOnProperty:指定的属性是否有指定的值
@ConditionalOnResource:类路径下是否有指定的资源
@ConditionalOnSingleCandidate:当指定的Bean在容器中只有一个,或者在有多个Bean的情况下,用来指定首选的Bean @ConditionalOnWebApplication:当前项目是Web项目的条件下

添加spring.factories

在resource/META-INF/下面创建spring.factories文件,内容为:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.tjm.springbootstartersample.AutoConfigure

至此已经完成starter的开发,运行 mvn:install 打包。

测试

引入starter依赖

<dependency>
<groupId>com.ltc</groupId>
<artifactId>sample-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>

修改配置文件

在application中添加以下配置:

example.show.enabled=true
example.config.str1=youxiu

使用JUnit进行单元测试

@Autowired
private WrapService wrapService; @Test
public void wrapServiceTest() {
String str = wrapService.wrap("222222222333333333");
System.out.println(str);
}

输出:222222222333333333youxiu

好,开发的第一个starter完成~

参考:https://www.cnblogs.com/yuansc/p/9088212.html

如何封装springboot的starter的更多相关文章

  1. SpringBoot自定义starter开发分布式任务调度实践

    概述 需求 在前面的博客<Java定时器演进过程和生产级分布式任务调度ElasticJob代码实战>中,我们已经熟悉ElasticJob分布式任务的应用,其核心实现为elasticjob- ...

  2. SpringBoot常用Starter介绍和整合模板引擎Freemaker、thymeleaf 4节课

    1.SpringBoot Starter讲解 简介:介绍什么是SpringBoot Starter和主要作用 1.官网地址:https://docs.spring.io/spring-boot/doc ...

  3. 手撸一个SpringBoot的Starter,简单易上手

    前言:今天介绍一SpringBoot的Starter,并手写一个自己的Starter,在SpringBoot项目中,有各种的Starter提供给开发者使用,Starter则提供各种API,这样使开发S ...

  4. SpringBoot --- 自定义 Starter

    SpringBoot --- 自定义 Starter 创建 1.需要创建一个新的空工程 2.新的工程需要引入两个模块 一个Maven 模块 作为启动器 一个SpringBoot 模块 作为自动配置模块 ...

  5. SpringBoot的starter到底是什么?

    前言 我们都知道,Spring的功能非常强大,但也有些弊端.比如:我们需要手动去配置大量的参数,没有默认值,需要我们管理大量的jar包和它们的依赖. 为了提升Spring项目的开发效率,简化一些配置, ...

  6. 如何使用SpringBoot封装自己的Starter

    作者:Sans_ juejin.im/post/5cb880c2f265da03981fc031 一.说明 我们在使用SpringBoot的时候常常要引入一些Starter,例如spring-boot ...

  7. SpringBoot封装自己的Starter

    https://juejin.im/post/5cb880c2f265da03981fc031 一.说明 我们在使用SpringBoot的时候常常要引入一些Starter,例如spring-boot- ...

  8. SpringBoot自定义starter及自动配置

    SpringBoot的核心就是自动配置,而支持自动配置的是一个个starter项目.除了官方已有的starter,用户自己也可以根据规则自定义自己的starter项目. 自定义starter条件 自动 ...

  9. SpringBoot自定义Starter实现

    自定义Starter: Starter会把所有用到的依赖都给包含进来,避免了开发者自己去引入依赖所带来的麻烦.Starter 提供了一种开箱即用的理念,其中核心就是springboot的自动配置原理相 ...

随机推荐

  1. JSP+java上传图片到服务器,并将地址保存至MYSQL + JSP网页显示服务器的图片

    这两天遇到个需求——用户头像修改功能. 查了好多资料,不是代码不全,就是某些高端框架,卡了好久,今已实现,分享给大家,如果有更好的方法,非常感谢可以在下方评论区写出 一.整体项目架构 二.web.xm ...

  2. spark 源码分析之十--Spark RPC剖析之TransportResponseHandler、TransportRequestHandler和TransportChannelHandler剖析

    spark 源码分析之十--Spark RPC剖析之TransportResponseHandler.TransportRequestHandler和TransportChannelHandler剖析 ...

  3. 工作笔记--对接三方Http接口遇到的问题

    在使用 HttpClient 4.4 调用第三方 http api 时遇到了很多问题,还好最后都解决了,记录一下遇到的问题及解决办法,希望对同样有此问题的你有所帮助. 环境说明 首先说明一点是,对方的 ...

  4. 应用性能测试神器 Gatling,你用过吗?

    在应用程序上线之前,有多少人做过性能测试? 估计大部分开发者更多地关注功能测试,并且会提供一些单元测试和集成测试的用例.然而,有时候性能漏洞导致的影响比未发现的业务漏洞更严重,因为性能漏洞影响的是整个 ...

  5. Shell基本语法---shell脚本的输入以及脚本拥有特效地输出

    shell脚本的输入 语法:read -参数 -p:给出提示符.默认不支持"\n"换行 -s:隐藏输入的内容 -t:给出等待的时间,超时会退出read,单位是秒 -n:限制读取字符 ...

  6. QRCode生成二维码,jq QRCode生成二维码,QRCode生成电子名片

    [QRCode官网]http://phpqrcode.sourceforge.net/ PHP QRCode生成二维码 官网下载QRCode源码包,引入源码包中的 qrlib.php . <?p ...

  7. javaweb入门-----jsp概念

    jsp是什么? JSP:Java Server Pages java服务器端页面 *可以理解为 一个特殊的页面,其中既可以直接定义html标签,又可以定义java代码 *用于简化书写 <% %& ...

  8. 堆排序(实现c++)

    堆可以看作是一个完全二叉树,分为大顶堆和小顶堆,本文我们以大顶堆为例来实现堆排序. (1)建堆 先把给定的序列转换成一棵完全二叉树,然后逐步对其调整使其每个结点的值都大于其两个子结点的值,因此我们需要 ...

  9. Unity经典游戏编程之:球球大作战

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  10. javascript基础案例解析

    学完了JavaScript基础部分,总结出一些基本案例,以备日后查看! 1.九九乘法口诀表:在控制台中输出九九乘法口诀表!代码如下: <!DOCTYPE html> <html> ...