如何写一个中间件的springboot的starter?
理解一下标题:一个springboot工程,只需要在配置文件配置相关中间件的信息,pom引入这个依赖,即可。
步骤:
- 新创建一个maven工程
- 引入springboot、中间件及相关pom依赖
- 编写配置类、自动装配类、服务类
- 编写一个spring.factories文件,把自动装配类路径写上
比如写一个elasticSearch的spring boot的starter
1.新创建一个maven工程
这里不过多赘述
2.引入springboot、中间件及相关pom依赖
我把我这里用的相关依赖贴一下,中间件的依赖不方便贴出来
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lvdouwanzi</groupId>
<artifactId>test-es-adapter</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.26</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<!-- jci要求禁用官方maven-jar-plugin插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>none</phase>
<inherited>true</inherited>
</execution>
</executions>
<configuration>
<skip>true</skip>
</configuration>
<inherited>true</inherited>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<outputDirectory>target\javadoc</outputDirectory>
<reportOutputDirectory>target\javadoc</reportOutputDirectory>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>releases</id>
<url>http://nexus.cbpmgt.com/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://nexus.cbpmgt.com/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</project>
3.编写配置类、自动装配类、服务类
3.1:配置类(EsClientProperties.java)
这里的bean此时不会注册到spring中,在自动装配类中会手动启用该配置类
package com.lvdouwanzi.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "jes")
public class EsClientProperties {
private String url;
private String appName;
private String secret;
public String getUrl() {
return url;
}
public String getAppName() {
return appName;
}
public String getSecret() {
return secret;
}
}
3.2服务类(EsSearchService.java)
package com.lvdouwanzi;
import com.lvdouwanzi.config.EsClientProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PreDestroy;
public class EsSearchService {
private final static Logger log = LoggerFactory.getLogger(EsSearchService.class);
private final EsClientProperties esClientProperties;
public EsSearchService(EsClientProperties esClientProperties) {
this.esClientProperties = esClientProperties;
init();
}
private void init() {
System.out.println("es client init");
}
@PreDestroy
public void destroy() {
System.out.println("es client destory");
}
}
3.3:自动装配类(EsAutoConfiguration.java)
注意:
- 这里也可以用ComponentScan去扫描该组件的类路径进行加载bean,也可以通过@bean注解去注入
- ConditionalOnProperty这里是规范,可以看n多中间件的starter都会有这个默认配置,这里可以理解是一个开关
- 构造方法会自动注入此时spring bean容器的类
package com.lvdouwanzi.start;
import com.lvdouwanzi.EsSearchService;
import com.lvdouwanzi.config.EsClientProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties({EsClientProperties.class})
@ConditionalOnClass({EsSearchService.class})
@ConditionalOnProperty(prefix = "es",
value = "enabled",
matchIfMissing = true)
public class EsAutoConfiguration {
private EsClientProperties esClientProperties;
EsAutoConfiguration(EsClientProperties esClientProperties) {
this.esClientProperties = esClientProperties;
}
@Bean
@ConditionalOnMissingBean({EsSearchService.class})
public EsSearchService esSearchService() {
return new EsSearchService(esClientProperties);
}
}
4.编写一个spring.factories文件,把自动装配类路径写上
resources 下新创建 META-INF 文件夹,文件夹新创建 spring.factories 文件
# Auto configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.lvdouwanzi.start.EsAutoConfiguration
|