springboot自定义Starter的具体流程
作者:技术-刘腾飞 时间:2022-01-26 05:08:06
自定义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
来源:https://www.cnblogs.com/frankltf/p/11396582.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java ArrayList实现删除指定位置的元素
![](https://img.aspxhome.com/file/2023/0/60160_0s.png)
Spring boot 集成Dubbox的方法示例
spring data jpa分页查询示例代码
Android异步消息机制详解
![](https://img.aspxhome.com/file/2023/0/85960_0s.png)
ThreadLocal使用案例_动力节点Java学院整理
深入理解Spring Boot的日志管理
解析Java线程同步锁的选择方法
java转树形结构工具类详解
详解Java中的ReentrantLock锁
Android Studio kotlin生成编辑类注释代码
![](https://img.aspxhome.com/file/2023/7/82417_0s.jpg)
Unity3D更改默认的脚本编辑器
![](https://img.aspxhome.com/file/2023/5/68625_0s.png)
Android中TextView动态设置缩进距离的方法
![](https://img.aspxhome.com/file/2023/4/86754_0s.png)
c# rsa加密解密详解
![](https://img.aspxhome.com/file/2023/4/68094_0s.png)
Java中final修饰的方法是否可以被重写示例详解
Java中List使用stream流转成map的几种方式详解
JAVA提高第八篇 动态代理技术
![](https://img.aspxhome.com/file/2023/9/62829_0s.jpg)
Maven实战之搭建Maven私服和镜像的方法(图文)
![](https://img.aspxhome.com/file/2023/9/60249_0s.png)
java实现双色球抽奖算法
![](https://img.aspxhome.com/file/2023/3/60543_0s.jpg)
flutter material widget组件之信息展示组件使用详解
Java迭代器与Collection接口超详细讲解
![](https://img.aspxhome.com/file/2023/9/66069_0s.png)