SpringBoot基础系列文章
简单
**、**快速
**地创建一个独立的、生产级别的Spring应用(说明:SpringBoot底层是Spring)少量配置
**即可快速整合Spring平台以及第三方技术SpringBoot特性
总结:简化整合、配置、开发、部署
pom.xml
spring-boot-starter-parent
**spring-boot-starter-web
**包含了web开发的所有依赖<?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>
<!-- 所有springboot项目都必须继承自 spring-boot-starter-parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.11</version>
<relativePath/>
</parent>
<artifactId>springboot</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- web开发的场景启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!--
SpringBoot应用打包插件
mvn clean package把项目打成可执行的jar包
java -jar demo.jar启动项目
-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
核心启动类
// 这是一个SpringBoot应用
@SpringBootApplication
public class SpringBootMainApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMainApplication.class,args);
}
}
Controller测试类
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "Hello,Spring Boot !";
}
}
访问路径:http://localhost:8080/hello
之前传统框架对依赖版本的选择具有**固定的搭配格式
**,并且这些依赖版本的选择还不能乱搭配。于是SpringBoot将**所有的技术版本
**的常见**使用方案(parent版本)
**都整理出来,以后开发者直接使用它提供的方案(parent版本)即可,就不用担心冲突问题了。
parent版本
**即为SpringBoot项目pom文件的**父工程坐标
版本号
**版本管理
**,**不会导入坐标
**<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.11</version>
</parent>
spring-boot-starter-parent
**坐标内容**
spring-boot-dependencies
**依赖管理,下面讲java编译版本
**以及**编码方式
**打包插件的版本
**,这也是SpringBoot的pom文件中打包插件不用写版本号的原因<dependencyManagement>
**依赖管理标签内,则表示只是引入申明,只有在**子pom
**中使用**<dependencies>(不用写版本号)
**依赖才会导入**导入依赖
**则需要在SpringBoot的pom文件中配置,如导入activemq<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-amqp</artifactId>
</dependency>
修改版本号
**,可以在SpringBoot的pom文件中配置<properties>
<activemq.version>5.7.0</activemq.version>
</properties>
# 或者直接修改版本,maven就近原则
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-amqp</artifactId>
<version>5.7.0</version>
</dependency>
starter
**定义了使用某种技术时对于依赖的**固定搭配格式
**,可以帮助开发者减少依赖配置** spring-boot-starter-*
***-spring-boot-starter
**spring-boot-starter-web
**的依赖<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
具体依赖的坐标
**spring-boot-starter-json
**和**spring-boot-starter-tomcat
**的内容spring-boot-autoconfigure
**的**jar
**包Spring容器对象(IoC容器)
**包及其子包
**@SpringBootApplication
public class SpringBootMainApplication {
public static void main(String[] args) {
//获取ioc容器
ConfigurableApplicationContext context = SpringApplication.run(SpringBootMainApplication.class, args);
//获取组件
User user = context.getBean(User.class);
System.out.println(user);
}
}
spring-boot-starter-tomcat
**说起<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.7.11</version>
<scope>compile</scope>
</dependency>
从上面说到的**spring-boot-autoconfigure
**的**jar
**包里的配置类,找到**ServletWebServerFactoryAutoConfiguration
**ServletWeb服务器工厂自动配置。
Tomcat
**类来自于**tomcat-embed-core-9.0.74.jar
**,由于导入这个jar,才能创建Tomcat容器工厂类,后续才能创建Tomcat容器并启动。**
创建Tomcat
启动Tomcat
挂起Tomcat,这也就是启动类main函数不会执行结束的原因