如何封装springboot的starter
--为啥要封装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的更多相关文章
- SpringBoot自定义starter开发分布式任务调度实践
概述 需求 在前面的博客<Java定时器演进过程和生产级分布式任务调度ElasticJob代码实战>中,我们已经熟悉ElasticJob分布式任务的应用,其核心实现为elasticjob- ...
- SpringBoot常用Starter介绍和整合模板引擎Freemaker、thymeleaf 4节课
1.SpringBoot Starter讲解 简介:介绍什么是SpringBoot Starter和主要作用 1.官网地址:https://docs.spring.io/spring-boot/doc ...
- 手撸一个SpringBoot的Starter,简单易上手
前言:今天介绍一SpringBoot的Starter,并手写一个自己的Starter,在SpringBoot项目中,有各种的Starter提供给开发者使用,Starter则提供各种API,这样使开发S ...
- SpringBoot --- 自定义 Starter
SpringBoot --- 自定义 Starter 创建 1.需要创建一个新的空工程 2.新的工程需要引入两个模块 一个Maven 模块 作为启动器 一个SpringBoot 模块 作为自动配置模块 ...
- SpringBoot的starter到底是什么?
前言 我们都知道,Spring的功能非常强大,但也有些弊端.比如:我们需要手动去配置大量的参数,没有默认值,需要我们管理大量的jar包和它们的依赖. 为了提升Spring项目的开发效率,简化一些配置, ...
- 如何使用SpringBoot封装自己的Starter
作者:Sans_ juejin.im/post/5cb880c2f265da03981fc031 一.说明 我们在使用SpringBoot的时候常常要引入一些Starter,例如spring-boot ...
- SpringBoot封装自己的Starter
https://juejin.im/post/5cb880c2f265da03981fc031 一.说明 我们在使用SpringBoot的时候常常要引入一些Starter,例如spring-boot- ...
- SpringBoot自定义starter及自动配置
SpringBoot的核心就是自动配置,而支持自动配置的是一个个starter项目.除了官方已有的starter,用户自己也可以根据规则自定义自己的starter项目. 自定义starter条件 自动 ...
- SpringBoot自定义Starter实现
自定义Starter: Starter会把所有用到的依赖都给包含进来,避免了开发者自己去引入依赖所带来的麻烦.Starter 提供了一种开箱即用的理念,其中核心就是springboot的自动配置原理相 ...
随机推荐
- CentOS7 部署K8S集群
虚拟机: VMware® Workstation 12 Pro 12.5.9 build-7535481操作系统:CentOS Linux release 7.6.1810 (Core) 部署 ...
- 通过ping命令了解三层转发流程
ping命令:因特网包探索器.本文主要通过路由器两端不同网段PC互ping来讲解三层转发流程. 例子:PC-A是如何 ping 通 PC-C 的,有几种情况? 说明:1.在条件1阶段PC-C不会刷新a ...
- firewalld防火墙命令规则设置
1.firewalld的基本使用 启动/关闭: systemctl start/stop firewalld 查看状态: systemctl status firewalld 开机启用/禁用 : sy ...
- 【iOS】containsString iOS7 报错
前几天发现了这个问题,原来是因为 containsString 只支持 iOS8.0 以后的系统,不支持 7... 有些地方可以用其他方法替代,如下: NSString *urlString = [[ ...
- [NSNull intValue]: unrecognized selector sent to instance 0x375c9860
今天遇到这个问题,程序崩溃了……日志如下: -[NSNull intValue]: unrecognized selector sent to instance 0x375c9860*** Termi ...
- Python基础总结之初步认识---clsaa类(上)。第十四天开始(新手可相互督促)
最近的类看着很疼,坚持就是胜利~~~ python中的类,什么是类?类是由属性和方法组成的.类中可能有很多属性,以及方法. 我们这样定义一个类: 前面是class关键字 后面school是一个类的名字 ...
- Intent 使用详解
极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android Intent 是一个消息传递对象,主要用于组建之间的通讯,例如:启动Activit ...
- 携程 Apollo 配置中心传统 .NET 项目集成实践
官方文档存在的问题 可能由于 Apollo 配置中心的客户端源码一直处于更新中,导致其相关文档有些跟不上节奏,部分文档写的不规范,很容易给做对接的新手朋友造成误导. 比如,我在参考如下两个文档使用传统 ...
- log4net服务启动后没有记录日志
有时候在用log4net的时候,调试或执行是ok的,但是安装服务后没有记录日志. 这是因为服务启动是在C盘启动,而程序放的位置在别的目录. 这时候需要指定读取配置文件的位置为程序所在的目录 strin ...
- MQ 服务器错误代码2035
MQ 服务器错误代码20352013-06-12 19:29:39 搭建一个MQ7.1服务器,用了一个小的demo测试程序,结果报错, 测试代码: import com.ibm.mq.MQC; imp ...