SpringBoot 自定义starter 原来这么简单
孙玉超
2020-09-04 15:11:30
评论
2144 浏览
收藏
0 赞
使用 SpringBoot 的开发人员应该都不陌生,我们一般会引入很多 starter,像 spring-boot-starter-data-redis 、 spring-boot-starter-web 、 spring-boot-starter-data-elasticsearch 等等。这些都是 SpringBoot 官方做的 starter,它们相对完美的封装了 redis ,es 等技术的使用。然而也有自己开发的优秀 starter,例如 mybatis-plus-boot-starter ,这是 mybatis-plus 官方定义的 starter,融入到 SpringBoot 中。当我们需要使用的时候,只需要引入 mybatis-plus 的 maven 坐标。就可以简单的通过配置使用 mybatis-plus。由此可见,我们也可以定制自己的 starter !
制作自己的 starter
首先建一个 maven 项目,使用 Spring Initializr 也可以,建好之后删掉不需要的就可以。我这里是下图的目录结构。
这里模仿 RedisTemplate , 写一个 HelloWorldTemplate ,注意 META-INF 下面的内容是固定的规范,SpringBoot 启动时会扫描所有包含这个文件的 Jar 包做自动配置。
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.syc</groupId>
<artifactId>helloworld-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>helloworld-spring-boot-starter</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional> <!--表示两个项目之间的依赖不传递-->
</dependency>
<!--为了确保能够生成元数据便于IDE进行配置属性的提示,需要引入一个依赖:-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<!--这个不能用springboot项目默认的,不然install报错-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>HelloWorldProperties.java
这个类主要是匹配 application.yml 里面定义的以 helloworld 为前缀的值,
@ConfigurationProperties(prefix = HelloWorldProperties.PREFIX)
@Data
public class HelloWorldProperties {
public static final String PREFIX= "helloworld";
/**
* 姓名,表示谁将会说这句话
* */
private String username;
}
HelloWorldTemplate.java
这个类是主要的功能类,要实现的具体功能
@Data
@Slf4j
public class HelloWorldTemplate {
private String username;
public String sayHello(){
return username+":HelloWorld";
}
}HelloWorldAutoConfiguration.java
这个类做自动配置,前面将 application.yml 中定义的配置注入到 HelloWorldProperties 中,这里把它再注入到主要功能类的对象中。
@Configuration
@EnableConfigurationProperties(HelloWorldProperties.class)
public class HelloWorldAutoConfiguration {
@Autowired
HelloWorldProperties helloWorldProperties;
@Bean
@ConditionalOnMissingBean(HelloWorldTemplate.class)
public HelloWorldTemplate helloWorldTemplate(){
HelloWorldTemplate helloWorldTemplate = new HelloWorldTemplate();
helloWorldTemplate.setUsername(helloWorldProperties.getUsername());
return helloWorldTemplate;
}
}spring.factories
这个文件是官方的规范
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.syc.helloworld.autoconfigure.HelloWorldAutoConfiguration
这样一来,自定义的 starter 就完成了,直接用 maven install 到本地仓库,然后在另一个项目中引入这个 starter
<!--引入自定义的starter-->
<dependency>
<groupId>com.syc</groupId>
<artifactId>helloworld-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>接着在 application.yml 配置
可以看到,不仅能提示,还能显示我们写的注释,这是因为在 starter 的 pom.xml 中引入了 processor 。
然后我们写个测试方法
这里就会根据刚刚在 application.yml 中配置的值进行返回。
为什么要自定义 starter
其实从 MyBatis-Plus 、 Redis 的 starter 就可以看出,因为多个项目都要用,那么如果不用 starter ,我们需要在每个项目都写同样的代码。而封装成 starter 之后,只需要在要使用的项目中引入 maven ,然后在 yml 文件中去配置就可以了。
总结
其实其他的非 SpringBoot 官方的 starter 都是类似的套路,就是把 application.yml 里面配置的值读取出来赋值给对象,然后交给 Spring 管理,这样我们使用的时候,直接从 Spring 容器里面拿过来就可以使用。
这里只是单纯的返回了一个字符串,实际上复杂的 starter 都是类似的套路,根据不同配置做不同的事情,只是在具体功能类中做了更复杂的业务逻辑而已。
哇,感觉好简单~~我目前也是初学者,以后有其他见解再来补充~~
待完善