SpringBoot基础系列文章
SpringBoot基础(一):快速入门
简单
、快速
地创建一个独立的、生产级别的Spring应用(说明:SpringBoot底层是Spring)少量配置
即可快速整合Spring平台以及第三方技术SpringBoot特性
总结:简化整合、配置、开发、部署
pom.xml
spring-boot-starter-parent
spring-boot-starter-web
包含了web开发的所有依赖 4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.11 springboot 8 8 UTF-8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-maven-plugin
核心启动类
// 这是一个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文件的父工程坐标
版本号
版本管理
,不会导入坐标
org.springframework.boot spring-boot-starter-parent 2.7.11
spring-boot-starter-parent
坐标内容
spring-boot-dependencies
依赖管理,下面讲java编译版本
以及编码方式
打包插件的版本
,这也是SpringBoot的pom文件中打包插件不用写版本号的原因
依赖管理标签内,则表示只是引入申明,只有在子pom
中使用(不用写版本号)
依赖才会导入导入依赖
则需要在SpringBoot的pom文件中配置,如导入activemq org.apache.activemq activemq-amqp
修改版本号
,可以在SpringBoot的pom文件中配置 5.7.0 # 或者直接修改版本,maven就近原则 org.apache.activemq activemq-amqp 5.7.0
starter
定义了使用某种技术时对于依赖的固定搭配格式
,可以帮助开发者减少依赖配置spring-boot-starter-*
*-spring-boot-starter
spring-boot-starter-web
的依赖 org.springframework.boot spring-boot-starter-web
具体依赖的坐标
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
说起 org.springframework.boot spring-boot-starter-tomcat 2.7.11 compile
从上面说到的spring-boot-autoconfigure
的jar
包里的配置类,找到ServletWebServerFactoryAutoConfiguration
ServletWeb服务器工厂自动配置。
Tomcat
类来自于tomcat-embed-core-9.0.74.jar
,由于导入这个jar,才能创建Tomcat容器工厂类,后续才能创建Tomcat容器并启动。
创建Tomcat
启动Tomcat
挂起Tomcat,这也就是启动类main函数不会执行结束的原因