4:SpringBoot-Starter-Java Spring

122 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

4.1 SpringBoot-Starter介绍

Starter是SpringBoot的一种服务,开发者不需要关注各种依赖库的处理和具体的配置信息

spring-boot-starter-web自动引入一些相关依赖和一些初始化的配置,Spring Boot将自动通过classpath路径下的类发现并加载需要的Bean

4.1 Starter原理

  • Starter实现自动化配置,需要Maven依赖(负责导入jar包)和配置文件
  • Spring Boot 启动时会在classpath寻找starter jar包中的resources/META-INF/spring.factories 配置文件
  • 根据配置文件,找到需要自动加载的类

在这里插入图片描述

没有Starter

  • 在Maven中引入使用的库
  • 引入使用的库所依赖的库
  • 在xxx.xml中配置一些属性信息
  • 反复的调试直到可以正常运行

使用Starter

  • 只需要引入一个Starter
  • Starter会把所有用到的依赖都给包含进来,避免了开发者自己去引入依赖所带来的麻烦

4.3 Starter依赖引入

要实现Starter,需要在构建配置文件中添加spring-boot-starter-web依赖项

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

例如在pom文件中添加对autoconfigure模块的依赖,并添加一些其他必要的依赖项

<dependency>
	<groupId>com.test.starter</groupId>
	<artifactId>xxx-spring-boot-autoconfigure</artifactId>
</dependency>

4.4 Starter配置

名称说明
spring-boot-starterCore starter, including auto-configuration support,logging and YAML (核心Spring Boot starter,包括自动配置支持,日志和YAML)
spring-boot-starter- aopStarter for aspect-oriented programming with Spring AOP and AspectJ(对面向切面编程的支持,包括spring-aop和AspectJ)
spring-boot-starter-cacheStarter for using Spring Framework’s caching support(使用 Spring Framework 的缓存支持)
spring-boot-starter- data-redisStarter for using Redis key-value data store with Spring Data Redis and the Lettuce client(用于将 Redis 键值数据存储与 Spring Data Redis 和 Lettuce 客户端一起使用)
spring-boot-starter-freemarkerStarter for building MVC web applications using FreeMarker views(对FreeMarker模板引擎的支持))
spring-boot-starter- quartzSpring Boot Quartz Starter(使用Spring Boot Quartz )
spring-boot-starter-securityStarter for using Spring Security(使用 Spring Security )
spring-boot-starter- testStarter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito(使用包括 JUnit、Hamcrest 和 Mockito 在内的库来测试 Spring Boot 应用程序的 Starter)
spring-boot-starter- webStarter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container(使用 Spring MVC 构建 Web 应用程序的入门程序,包括 RESTful 应用程序。 使用 Tomcat 作为默认的嵌入式容器)