如何使用SpringBoot自定义starter
这篇文章主要介绍了如何使用SpringBoot自定义starter,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
springboot是什么
springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。
一、新建一个工程
工程由xxx-sprig-boot-starter和xxx-sprig-boot-starter-configure两个模块组成;
xxx-sprig-boot-starter模块
只用来做依赖导入
依赖于
xxx-sprig-boot-starter-configure模块,没有实际代码
4.0.0 com.ander ander-spring-boot-starter 1.0-SNAPSHOT com.ander ander-spring-boot-starter-configure 0.0.1-SNAPSHOT
xxx-sprig-boot-starter-configure模块
专门自动配置模块
依赖于
spring-boot-starter-web
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.10.RELEASE com.ander ander-spring-boot-starter-configure 0.0.1-SNAPSHOT ander-spring-boot-starter-configure DemoprojectforSpringBoot 1.8 org.springframework.boot spring-boot-starter-web
二、xxx-sprig-boot-starter-configure模块自动配置编码
2.1 服务层编码
/**
*Service层
*
*@Author:Ander
*@Date:2021-05-04
*/
publicclassHelloService{
privateHelloServicePropertieshelloServiceProperties;
publicStringhelloService(Stringname){
returnhelloServiceProperties.getPrefix()+""+name+""+helloServiceProperties.getSuffix();
}
publicHelloServicePropertiesgetHelloServiceProperties(){
returnhelloServiceProperties;
}
publicvoidsetHelloServiceProperties(HelloServicePropertieshelloServiceProperties){
this.helloServiceProperties=helloServiceProperties;
}
}2.2 属性配置类编码
/**
*属性配置类
*
*@Author:Ander
*@Date:2021-05-04
*/
@ConfigurationProperties(prefix="com.ander")
publicclassHelloServiceProperties{
privateStringprefix="hi";
privateStringsuffix="helloworld";
publicStringgetPrefix(){
returnprefix;
}
publicvoidsetPrefix(Stringprefix){
this.prefix=prefix;
}
publicStringgetSuffix(){
returnsuffix;
}
publicvoidsetSuffix(Stringsuffix){
this.suffix=suffix;
}
}2.3 starter自动配置类编码
@EnableConfigurationProperties({HelloServiceProperties.class})作用:让xxxProperties生效加入到容器中
/**
*自定义starter自动配置类
*
*@Date:2021-05-04
*/
@Configuration
@ConditionalOnWebApplication//指定web应用才生效
@EnableConfigurationProperties({HelloServiceProperties.class})
publicclassHelloServiceAutoConfigure{
@Autowired
privateHelloServicePropertieshelloServiceProperties;
@Bean
publicHelloServicehelloService(){
HelloServicehelloService=newHelloService();
helloService.setHelloServiceProperties(helloServiceProperties);
returnhelloService;
}
}2.4 添加自动配置类到META-INF路径下
2.5 将工程安装到本地
注意先安装xxx-spring-boot-starter-configure,再安装xxx-spring-boot-starter
三、新建一个工程测试自定义starter
3.1 编写controller层
/**
*starter测试控制类
*
*@Author:Ander
*@Date:2021-05-05
*/
@RestController
publicclassStarterTestController{
@Autowired
privateHelloServicehelloService;
@GetMapping("hello")
publicStringhello(Stringname){
returnhelloService.helloService(name);
}
}3.2 编写配置文件
server.port=8888com.ander.prefix=HIcom.ander.suffix=HELLO WORLD
四、测试结果
4.1 使用starter默认配置
4.2 使用自定义配置
感谢你能够认真阅读完这篇文章,希望小编分享的“如何使用SpringBoot自定义starter”这篇文章对大家有帮助,同时也希望大家多多支持恰卡编程网,关注恰卡编程网行业资讯频道,更多相关知识等着你来学习!
推荐阅读
-
vue动态添加删除输入框(springboot vue怎么让数据库显示出来)
springbootvue怎么让数据库显示出来?一般情况下是前端调阅后端接口,来获取到数据库的数据,后端哪里会把数据库的数据整理...
-
springboot实现基于aop的切面日志
本文实例为大家分享了springboot实现基于aop的切面日志的具体代码,供大家参考,具体内容如下通过aop的切面方式实现日志...
-
SpringBoot定时任务功能怎么实现
-
SpringBoot中的@Import注解怎么使用
-
SpringBoot整合Lombok及常见问题怎么解决
SpringBoot整合Lombok及常见问题怎么解决这篇文章主要...
-
springboot图片验证码功能模块怎么实现
springboot图片验证码功能模块怎么实现本篇内容主要讲解“s...
-
Springboot+SpringSecurity怎么实现图片验证码登录
-
SpringBoot注解的知识点有哪些
SpringBoot注解的知识点有哪些这篇“SpringBoot注...
-
SpringBoot2.x中management.security.enabled=false无效怎么解决
SpringBoot2.x中management.security.enabled=false无效怎么解决...
-
springboot怎么禁用某项健康检查
springboot怎么禁用某项健康检查今天小编给大家分享一下sp...
