工作中做了一个短信的聚合服务,使用了starter的模式。在项目中直接引入,加上发送注解,直接调用发送短信服务即可。
放到了github上:
https://github.com/lucien-kejl/sms-service
开发过程如下:
建立Maven项目,引入依赖
<?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.example</groupId>
<artifactId>example-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
这里说下artifactId
的命名问题,Spring 官方 Starter通常命名为spring-boot-starter-{name}
如 spring-boot-starter-web
, Spring官方建议非官方Starter命名应遵循{name}-spring-boot-starter
的格式。
编写配置类
@ConfigurationProperties("example.service")
public class ExampleServiceProperties {
private String prefix;
private String suffix;
//省略 getter setter
编写自动配置类
@Configuration
@ConditionalOnClass(ExampleService.class)
@EnableConfigurationProperties(ExampleServiceProperties.class)
public class ExampleAutoConfigure {
@Autowired
private ExampleServiceProperties properties;
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "example.service",value = "enabled",havingValue = "true")
ExampleService exampleService (){
return new ExampleService(properties.getPrefix(),properties.getSuffix());
}
}
解释下用到的几个和Starter相关的注解:
-
@ConditionalOnClass
,当classpath
下发现该类的情况下进行自动配置。 -
@ConditionalOnMissingBean
,当Spring Context
中不存在该Bean
时。 -
@ConditionalOnProperty(prefix = "example.service",value = "enabled",havingValue = "true")
,当配置文件中example.service.enabled=true
时。最后一步,在
resources/META-INF/
下创建spring.factories
文件,内容供参考下面
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.autocinfigure.ExampleAutoConfigure
这个是springboot加载过程中读取的配置类,运行 mvn:install
打包安装。以上就可以完成一个简单的starter
扩展
本地依赖如何打包
我编写的短信组件是包含有阿里云的短信服务的,此时需要在maven 本地 中引入一个jar包
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.3</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/aliyun-java-sdk-core-4.5.1.jar</systemPath>
</dependency>
这个时候打出来的包是无法到starter中的。
在pom中 增加配置
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compile.source>1.8</maven.compile.source>
<maven.compile.target>1.8</maven.compile.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${maven.compile.source}</source>
<target>${maven.compile.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<!-- 打包时不打包jdbc配置 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>/META-INF/spring.factories</include>
</includes>
</resource>
</resources>
</build>
assembly.xml 放到项目的根目录下即可
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>system</scope>
</dependencySet>
</dependencySets>
</assembly>
这个时候再 maven install 就会产生
gykj-sms-springboot-starter-1.0-SNAPSHOT-jar-with-dependencies.jar 含有全部依赖的jar包。
打开这个压缩包 image-20220222190459489.png改为自己的
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.autocinfigure.ExampleAutoConfigure
就可以了。
反思
中间日志记录没有仔细看,还以为是没有注入进来的情况,结果是包依赖没有加入进来。唉...
网友评论