1、starter介绍
简介: 可插拔插件
与jar包区别: starter能实现自动配置
作用: 大幅提高开发效率
2、常用的starter
名称 | 描述 |
spring-boot-starter-thymeleaf | 使MVC Web applications 支持Thymeleaf |
spring-boot-starter-mail | 使用Java Mail、Spring email发送支持 |
spirng-boot-starter-data-redis | 通过Spring Data Redis、Jedis client使用Redis键值存储数据库 |
spirng-boot-starter-web | 构建web,包含RESTful风格架构SpringMVC和默认的切入式容器Tomcat |
spirng-boot-starter-activermq | 为JMS使用Apache ActiveMQ |
spirng-boot-starter-data-elasticsearch | 使用Elasticsearch、analytics engine、Spring Data Elasticsearch |
spirng-boot-starter-aop | 通过Spring AOP、AspectJ面向切面编程 |
spirng-boot-starter-security | 使用Spring Security |
spirng-boot-starter-data-jpa | 通过Hibernate使用Spring Data JPA |
spirng-boot-starter | Core starter,包括自动配置支、logging and YAML |
spirng-boot-starter-freemarker | 使MVC Web Applications支持FreeMarker |
spirng-boot-starter-batch | 使用Spring Batch |
spirng-boot-starter-solr | 通过Spring Data Solr使用Apache Solr |
spirng-boot-starter-mongodb | 使用MongoDB文件存储数据库、Spring Data MongoDB |
3、自定义starter
1、新建工程
创建成功后,增加自动配置
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.alen</groupId>
<artifactId>spring-boot-starter-price</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--
springboot的spring-boot-autoconfigure模块通过灵活的Auto-configuration注解
使SpringBoot中的功能实现模块化和可被替换扩展。
spring-boot-autoconfigure思路类似SPI(Service Provider Interface),
都是不同的实现类实现了定义的接口
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2) 创建PriceSource类(属性源)
@ConfigurationProperties(prefix = "price")
public class PriceSource {
private String type;
private String msg;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
3) 创建PriceService服务,供外部调用
public class PriceService {
private PriceSource priceSource;
public PriceService(PriceSource priceSource) {
this.priceSource = priceSource;
}
public String getType() {
return priceSource.getType();
}
public String getMsg() {
return priceSource.getMsg();
}
}
4)创建PriceAutoConfiguration。增加注解EnableConfigurationProperties, ConditionalOnProperty。属性weather.enable值为enable才会自动注入
@Configuration
@EnableConfigurationProperties(PriceSource.class)
@ConditionalOnProperty(name = "price.enable", havingValue = "enable") //属性weather.enable值为enable才会自动注入
public class PriceAutoConfiguration {
@Autowired
private PriceSource priceSource;
@Bean
@ConditionalOnMissingBean(PriceService.class)
public PriceService priceService(){
return new PriceService(priceSource);
}
}
ConditionalOnMissingBean注解下方法的作用: 当引用方没有手工定义PriceService的时候,我们自动注入PriceService
5) spring.factories中添加自动配置类实现
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.alen.redission.config.PriceAutoConfiguration
在另一个工程中使用这个starter
1)我们在sb2工程中引入这个jar包
<dependencies>
<!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->
<!-- spring mvc (springboot 默认集成) springboot-web依赖即可 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 去掉springboot默认配置 -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</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>
<dependency>
<groupId>com.alen</groupId>
<artifactId>spring-boot-starter-price</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
2)application.yml增加配置如下
price:
enable: enable
type: order
msg: 订单金额
3)测试类
@SpringBootTest
class SpringBootSourceApplicationTests {
@Autowired
private PriceService priceService;
@Test
void contextLoads() {
System.out.println("---Type--" + priceService.getType() + "---Msg----" + priceService.getMsg());
}
}
4) 运行结果如下
---Type--order---Msg----订单金额
5)修改配置文件price.enable=disable
会报错