什么时候需要创建自定义starter
在我们的日常开发中,可能会需要开发一个通用模块,以供其它工程复用。这时我们可以把通用模块封装成tarter,这样其它工程复用的时候只需在pom中引用依赖,由SpringBoot为我们完成自动装配。
怎么样定义starter
通过Springboot的AutoConfiguration机制,尝试自定义SpringBoot starter。
了解springboot是如何加载starter的,也就是Springboot自动装配机制
自定义starter的命名规范
spring-boot-starter-xxx是SpringBoot官方定义的jar,如spring-bbot-starter-web。
xxx-spring-boot-starter是非官网定义的,如第三方jar包druid-spring-boot-starter。
参考第三方starter自定义个人starter
以druid-spring-boot-starter为例
当导入了 druid-spring-boot-starter后,其导入的相关依赖如下:
![](https://i-blog.csdnimg.cn/blog_migrate/e678f3d1899d434854de290f3737fdec.png)
可以看到其导入了 druid-spring-boot-autoconfigure的jar包,其中有一个自动配置类DruidDataSourceAutoConfigure ,如图
![](https://i-blog.csdnimg.cn/blog_migrate/3bbbd490c955597136d9cdac8342c3ee.png)
注解@Configuration和@Bean注解,其结合使用可以替代传统的xml配置文件,@Bean把DataSource注入到Spring容器。
注解@EnableConfigurationProperties,其作用是让后面指定的配置属性生效。
注解@ConditionalOnClass,当项目中存在某个类时才会使标有该注解的类或方法生效;
注解@AutoConfigureBefore,该注解的值会在标有该注解的类之前执行;
注解@Import,导入配置类;
在SpringBoot进行自动装配时,它会选择性的读取META-INF/spring.factories配置文件。
其中key是固定的,value是指定配置类的全路径。
![](https://i-blog.csdnimg.cn/blog_migrate/4fc6cac398c053cb2cf75fc7e827689f.png)
自定义我的starter
1、创建一个空的maven工程
![](https://i-blog.csdnimg.cn/blog_migrate/9a155a7dec758466bee5e927f4fa2707.png)
![](https://i-blog.csdnimg.cn/blog_migrate/38887ec8c56ad02af79fdb0fc3b0ccb9.png)
2、引入pom依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
3、读取并注入配置信息
创建MqProperties配置属性类
package org.intl.util;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "hello")
public class MqProperties {
private String mqHost="localhost";
private String mqUsername="root";
private String mqPassword="123456";
public String getMqHost() {
return mqHost;
}
public void setMqHost(String mqHost) {
this.mqHost = mqHost;
}
public String getMqUsername() {
return mqUsername;
}
public void setMqUsername(String mqUsername) {
this.mqUsername = mqUsername;
}
public String getMqPassword() {
return mqPassword;
}
public void setMqPassword(String mqPassword) {
this.mqPassword = mqPassword;
}
}
创建MqService服务类
package org.intl.service;
import org.intl.util.MqProperties;
import org.springframework.beans.factory.annotation.Autowired;
public class MqService {
@Autowired
private MqProperties mqProperties;
public String getMq() {
return mqProperties.getMqHost() + "," + mqProperties.getMqUsername() + "," + mqProperties.getMqPassword();
}
}
创建MqAutoConfiguration自动配置类,注入MqService到Spring中。
package org.intl.config;
import org.intl.service.MqService;
import org.intl.util.MqProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(MqProperties.class)
public class MqAutoConfiguration {
@Bean
public MqService mqService(){
return new MqService();
}
}
在resources目录下,创建文件META-INF\spring.factories,指定自动配置类的路径
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.intl.config.MqAutoConfiguration
项目打包发布到maven仓库
![](https://i-blog.csdnimg.cn/blog_migrate/ccb3ce71049b3c8c8783f5025056f54d.png)
引入自定义starter
新建一个SpringBoot的项目,导入依赖
![](https://i-blog.csdnimg.cn/blog_migrate/5bf6b32cba89b1311bd1eda8e39f6f9d.png)
配置文件进行配置
![](https://i-blog.csdnimg.cn/blog_migrate/035eb8dc999e33eb60eacf2ba1246bb9.png)
编写测试接口
![](https://i-blog.csdnimg.cn/blog_migrate/c361a8e25f028fd16722a3def1fb631e.png)
启动运行
![](https://i-blog.csdnimg.cn/blog_migrate/a79cbbffc40a6a1c9493911f4943033e.png)