在不使用springboot开发项目的时候,或者说springboot还没有出来的时候。我们要在项目中引用某一个功能,比如redis。
在一个完整的项目中,还会引入很多其他的jar包,要和spring进行整合。每一种jar包都要把上面的步骤重复一般,是很繁琐的。
springboot的中的starter机制就是为了解决上述问题的。
我们先来看看starter长什么样


里面有一个mybatis-spring-boot-autoconfigure的包。看看这个包里面的内容
重点看看MybatisProperties、MybatisAutoConfiguration、spring.factories这三个文件

@ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)
public class MybatisProperties {
public static final String MYBATIS_PREFIX = "mybatis";
从上面这几行代码可以看出,mybatis的配置是以 mybatis开始的,大概向下面这样



上面截图只截了一部分,类上面的注解决定这个配置类是否生效。最重要的是这个配置类中生成了我们经常使用的SqlSessionFactory这个Bean。


我们参照mybatis-spring-boot-starter的实现步骤编写一个Starter。该Starter封装了多个消息发送渠道,业务模块只需引入该starter并配置好使用的渠道就可实现消息发送功能。
首先创建一个工程,包含三个子模块 example-spring-boot-starter、example-spring-boot-autoconfigure、example-spring-boot-test,目录结构如下:

先来看看 example-spring-boot-autoconfigure ,这是功能实现的模块
ExampleProperties类
@ConfigurationProperties(prefix = ExampleProperties.EXAMPLE_PREFIX)
public class ExampleProperties {
public static final String EXAMPLE_PREFIX = "example";
private Integer type;
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
}
这个类提供了使用者可配置的属性。我们这里就一个简单的type,及消息发送渠道类型。
ExampleMessageSendService类
public class ExampleMessageSendService {
private static Map<Integer,String> messageMap = new HashMap<>();
static {
messageMap.put(1,"短信消息");
messageMap.put(2,"微信公众号消息");
messageMap.put(3,"邮件消息");
}
private Integer type;
public ExampleMessageSendService(Integer type){
this.type = type;
}
public void sendMsg(){
if(!messageMap.containsKey(type)){
System.out.println("当前消息渠道未实现");
return;
}
System.out.println("发送" + messageMap.get(type) + "成功");
}
}
这个类是具体的功能逻辑实现类,根据使用者的配置信息选择消息渠道发送消息。注意,这个类只是一个普通的类,没有使用@Service等注解。是在配置类中被加载为Bean的。
ExampleAutoConfigure类
@ConditionalOnClass(ExampleProperties.class)
@EnableConfigurationProperties(ExampleProperties.class)
@Configuration
public class ExampleAutoConfigure {
@Autowired
private ExampleProperties properties;
@Bean
public ExampleMessageSendService exampleMessageSendService(){
return new ExampleMessageSendService(properties.getType());
}
}
这个类是配置类,自动配置功能会扫描到它。它的主要功能就是生成功能实现类的Bean,并将该Bean和属性配置类联系起来。
spring.factories 文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.autoconfigure.ExampleAutoConfigure
这个文件指定 ExampleAutoConfigure 类是需要被扫描的配置类。
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>example</artifactId>
<groupId>springboot-starter-example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>example-spring-boot-autoconfigure</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
</dependencies>
</project>
example-spring-boot-starter模块中没有功能实现,只在pom文件中引入了example-spring-boot-autoconfigure模块
<?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">
<parent>
<artifactId>example</artifactId>
<groupId>springboot-starter-example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>example-spring-boot-starter</artifactId>
<dependencies>
<dependency>
<groupId>springboot-starter-example</groupId>
<artifactId>example-spring-boot-autoconfigure</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
测试模块。
先在pom.xml文件中引入starter:
<?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">
<parent>
<artifactId>example</artifactId>
<groupId>springboot-starter-example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>example-spring-boot-test</artifactId>
<dependencies>
<dependency>
<groupId>springboot-starter-example</groupId>
<artifactId>example-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Application.yml配置
example:
type: 1
Application启动类:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
TestRuuner类,在项目启动后验证功能
@Component
public class TestRuuner implements ApplicationRunner {
@Autowired
private ExampleMessageSendService exampleMessageSendService;
public void run(ApplicationArguments args) throws Exception {
exampleMessageSendService.sendMsg();
}
}
以上,就是SpringBoot Starter的分析以及自己编写Starter的实现。