搜索
首页Javajava教程怎么在springboot中自定义Starter

怎么在springboot中自定义Starter

May 28, 2023 pm 03:25 PM
springbootstarter

自定义Starter命名规则

注意artifactId的命名规则,Spring官方Starter通常命名为spring-boot-starter-{name}如 spring-boot-starter-web, Spring官方建议非官方Starter命名应遵循{name}-spring-boot-starter的格式, 如mybatis-spring-boot-starter。这里创建的项目的artifactId为helloworld-spring-boot-starter

开发Starter步骤

  • 创建Starter项目

  • 定义Starter需要的配置(Properties)类

  • 编写自动配置类

  • 编写spring.factories文件加载自动配置类

  • 编写配置提示文件spring-configuration-metadata.json(不是必须的)

具体流程

创建配置类

@ConfigurationProperties 来定义配置的前缀

@EnableConfigurationProperties(InfluxdbProperties.class)
@ConfigurationProperties(prefix = "spring.influxdb")
public class InfluxdbProperties {
 private String username;
 public String getDatabase() {
   return database;
 }
 public void setDatabase(String database) {
   this.database = database;
 }
}

编写自动配置类

  • @EnableConfigurationProperties配置依赖的属性类

  • @ConditionalOnProperty 配置Configuration的加载规则

    • value 指的是Properties的哪个字段

    • havingValue指的是配置value是什么值的时候加载Configuration

    • matchIfMissing 指的是当value配置的字段没有配置时的默认值

  • @Bean 配置自动注入的bean

  • springboot特有的常见的条件依赖注解有:

    • @ConditionalOnBean,仅在当前上下文中存在某个bean时,才会实例化这个Bean。

    • @ConditionalOnClass,某个class位于类路径上,才会实例化这个Bean。

    • @ConditionalOnExpression,当表达式为true的时候,才会实例化这个Bean。

    • @ConditionalOnMissingBean,仅在当前上下文中不存在某个bean时,才会实例化这个Bean。

    • @ConditionalOnMissingClass,某个class在类路径上不存在的时候,才会实例化这个Bean。

    • @ConditionalOnNotWebApplication,不是web应用时才会实例化这个Bean。

    • @AutoConfigureAfter,在某个bean完成自动配置后实例化这个bean。

    • @AutoConfigureBefore,在某个bean完成自动配置前实例化这个bean。

@Configuration
@Order(1)
@EnableConfigurationProperties(InfluxdbProperties.class)
@ConditionalOnClass(InfluxdbProperties.class)
@ConditionalOnProperty(prefix = "spring.influxdb", value = "use-influxdb", havingValue="true" ,matchIfMissing = false)
public class InfluxdbAutoConfiguration {
private String scanEntitySuffix = "Entity.class";

@Bean
@ConditionalOnMissingBean(AiInfluxdbTemplate.class)
@Order(Ordered.HIGHEST_PRECEDENCE)
public AiInfluxdbTemplate AiInfluxdbTemplate(InfluxdbProperties influxdbProperties){
  return new AiInfluxdbTemplate(influxdbProperties);
}
}

编写spring.factories文件

Spring Boot会默认扫描跟启动类平级的包,如果我们的Starter跟启动类不在同一个主包下,需要通过配置spring.factories文件来生效

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ai.base.boot.influxdb.InfluxdbAutoConfiguration

以上是怎么在springboot中自定义Starter的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用