- 修改端口号(配置在application.properties中)
server.port=8081
原理
学习文章:SpringBoot自动配置的原理及实现_牧竹子-CSDN博客_springboot自动配置的原理
自动配置:
pom.xml
- spring-boot-dependencies:核心依赖在父工程中!
- 在写或者引入一些Springboot依赖的时候,不需要指定版本,版本信息是由 spring-boot-starter-parent(版本仲裁中心) 统一控制的。
启动器 spring-boot-starter
starter 都遵循着约定成俗的默认配置,并允许用户调整这些配置,即遵循“约定大于配置”的原则。(springboot启动场景,比如spring-boot-starter-web,就会自动导入web环境所有的依赖)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
在该项目中执行以下 mvn 命令查看器依赖树。
mvn dependency:tree
父级依赖 spring-boot-starter-parent
spring-boot-starter-parent 是所有 Spring Boot 项目的父级依赖,它被称为 Spring Boot 的版本仲裁中心,可以对项目内的部分常用依赖进行统一管理。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Spring Boot 项目可以通过继承 spring-boot-starter-parent 来获得一些合理的默认配置,它主要提供了以下特性:
- 默认 JDK 版本(Java 8)
- 默认字符集(UTF-8)
- 依赖管理功能
- 资源过滤
- 默认插件配置
- 识别 application.properties 和 application.yml 类型的配置文件
主程序
//@SpringBootApplication :标注这个类是一个springboot的应用
@SpringBootApplication
public class Springboot01HelloworldApplication {
public static void main(String[] args) {
//将springboot应用启动
SpringApplication.run(Springboot01HelloworldApplication.class, args);
}
}
- 注解
- @SpringBootApplication:springboot的配置
- @Configuration:spring配置类
- @Component:说明这也是一个spring的组件
- @EnableAutoConfiguration:自动配置
- @SpringBootApplication:springboot的配置
SpringApplication
- 推断应用的类型是普通的项目还是web项目
- 查找并加载所有可用的初始化器,设置到 initializers属性中
- 找出所有的应用程序监听器,设置到listeners属性中
- 推断并设置main方法的定义类,找到运行的主类