怎么在springboot中自定义Starter
自定义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步骤创建Starte
自定义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
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















