自訂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步驟
建立配置類別
@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; } }
@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 Boot會預設掃描跟啟動類別平級的包,如果我們的Starter跟啟動類不在同一個主套件下,需要透過設定spring.factories檔案來生效
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.ai.base.boot.influxdb.InfluxdbAutoConfiguration
以上是怎麼在springboot自訂Starter的詳細內容。更多資訊請關注PHP中文網其他相關文章!