一、starter
starter=依赖+配置
一个starter就是一个开发场景的支持
springboot为我们提供了简化企业级开发绝大多数场景的支持(提供了多个starter),我们在进行项目开发的过程中只需引入对应的的starter(创建SpringBoot应用时可选择),相关的依赖和配置就会被内置到项目中(消除人工配置)。
二、starter依赖
一个starter依赖表示的不是一个依赖,而是某种开发环境所需的一组依赖
例如:
- Spring web -----------spring-boot-starter-web
-
MyBatis Framewwork---------mybatis-spring-boot-starter
starter 配置:
一个starter不仅包含所需依赖还包含了其所需的对应的配置
例如:MyBatis Framewwork----mybatis-spring-boot-starter
- 依赖
- 配置
三、案例
引入redis开发场景
- 添加starter依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- 在service中可以直接注入redis客户端
@Service public class UserServiceImpl implements UserService { @Resource private UserDAO userDAO; @Resource private SimpleResultTreeImpl simpleResultTree; }
四、SpringBoot 应用的pom文件
4.1 基于Spring官方服务器创建的SpringBoot应用
- 继承spring-boot-starter-parent.pom
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- springboot应用中的pom 继承spring-boot-starter-parent.pom -->
<!-- 继承spring-boot-starter-parent.pom 又继承了spring-boot-dependencies.pom-->
<!-- 在spring-boot-dependencies.pom已经对主流的框架的版本进行了声明 -->
<dependencyManagment>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.6.7</version>
</dependency>
</dependencyManagment>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/>
</parent>
- 引入了maven对SpringBoot应用支持的插件spring-boot
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.example.springbootdemo1.SpringbootDemo1Application</mainClass>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
4..2 基于aliyun官方服务器创建的SpringBoot应用
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>springboot--demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot--demo2</name>
<description>springboot--demo2</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- springboot引用的pom没有继承spring-boot-starter-parent.pom因此,版本需要在当前pom中进行定义-->
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<mainClass>com.example.springboot.demo2.SpringbootDemo2Application</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
五、java的配置方式
如果我们需要在SpringBoot应用中整合一种新的开发场景,只需要在pom.mxl引入对应的starter即可一个starter不仅包含依赖,还包含相应的配置,starter中包含的配置都是通过Java类实现的------java配置方式 。
5.1spring版本发展
随着Spring版本的迭代,配置方式也在发生变化
- Spring 1.x:所有的bean的配置只能通过xml完成
- Spring 2.x :1、基于JDK1.5对注解的支持,Spring 2.x开始支持注解2、 企业开发中到底是用xml配置还是用注解?:1.对基础配置、引用的第三方依赖中的配置使用xml完成:例如数据源配置2、业务开发使用注解:例如:controller、service
- Spring 3.x :Spring开始提供基于Java的配置方式
5.2 xml的配置
<!-- applicationContext.xml -->
<bean id="stu" class="com.qfedu.beans.Student"></bean>
5.3 注解配置
@Component
public class Student{
}
5.4java配置方式
public class Student{
}
- 创建配置类
@Configguration
public class SpringConfig{
@Bean
public Student getStudent(){
return new Student();
}
}