Starter使得编写程序就像搭积木一样,这也是springboot很火的一个重要原因吧。
Starter相当于模块,包括:
1.这个模块需要的依赖库;
2.提供对模块的配置项给使用者;
3.提供自动配置类对模块内的Bean进行自动装配;
我们在POM中引入starter,Spring Boot能自动扫描并加载相应的模块。
1 starter原理
SpringBoot自动配置的原理:
第1步,SpringBoot 在启动时会去依赖的starter包中寻
找 resources/META-INF/spring.factories 文件,然后根据文件中配置的Jar包去扫描项目所依赖的Jar包,这类似于 Java 的 SPI 机制。
第2步,根据 spring.factories配置加载AutoConfigure类。
第3步,根据 @Conditional注解的条件,进行自动配置并将Bean注入Spring Context 上下文当中。
2 starter的结构
以spring-boot-starter-redis为例进行说明:
我们POM文件中引入spring-boot-starter-redis:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>${spring-boot-starter-redis-version}</version>
</dependency>
我们可以看到spring-boot-starter-redis是一个空的工程,
spring-boot-starter-redis工程POM文件中引入了spring-data-redis的工程。
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
spring-data-redis的工程包含了自动配置的类。
小结:springboot Starter包含starter project和自动配置project;
- starter project 是一个空的jar文件,仅提供辅助性依赖管理,会引用自动配置project
- 自动配置project 按照自动注解实现自动配置类,在META-INF/spring.factories中配置自动配置的类。
3 自定义使用starter
本节建立一个project演示starter的使用,并输出HellowWorld!
projec包含两个module,starter module和webmodule。starter module包含了自动配置模类,web module依赖starter module。
3.1 starter module
第1步:POM中引入:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
第2步:定义配置的类:
@ConfigurationProperties(prefix = "starter")
public class StartProperties {
private String config;
public String getConfig() {
return config;
}
public void setConfig(String config) {
this.config = config;
}
}
第3步: 定义一个service:
public class StartService {
private StartProperties startProperties;
public StartProperties getStartProperties() {
return startProperties;
}
public void setStartProperties(StartProperties startProperties) {
this.startProperties = startProperties;
}
public String print(){
return startProperties.getConfig();
}
}
第4步:定义一个自动配置的类:
@Configuration
@ConfigurationProperties //web应用才生效
@EnableConfigurationProperties(StartProperties.class)
public class StartAutoConfiguration {
@Autowired
private StartProperties startProperties;
@Bean
public StartService helloService() {
StartService startService = new StartService();
startService.setStartProperties(startProperties);
return startService;
}
}
第5步:META-INF的spring.factories文件配置StartAutoConfiguration:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.feidao.starter.StartAutoConfiguration
3.2 web module
第1步:设置web module依赖starter module;
第2步:在application.properties 中加上配置:
starter.config=HelloWorld!
第3步:定义WebController类方便验证:
@RestController
@RequestMapping(value = "/starter")
public class WebController {
@Autowired
private StartService startService;
@RequestMapping(value = "/print")
public String print(){
String str = startService.print();
return str;
}
}
第4步:启动web module,执行localhost:8080/starter/print验证如下:
具体参考代码:
https://gitee.com/alifeidao/springboot-simple/tree/master/
chapter2
教程列表:
springboot simple(0) springboot简介
springboot simple(1) springboot Helloworld
springboot simple(2) springboot Starter
springboot simple(3 )springboot Web开发
springboot simple(4)springboot 数据持久化
springboot simple (5) springboot Nosql
springboot simple (6) springboot mqtt
springboot simple (7) springboot thrift
springboot simple (8) springboot kafka
springboot simple (9) springboot jpa(Hibernate)
springboot simple (10) springboot protobuf
springboot simple (11) springboot protostuff
springboot simple (12) springboot RabbitMQ