Spring Boot项目结构解析
理解 Spring Boot 项目核心文件的作用。
pom.xml解析
基本结构
XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<!-- 继承Spring Boot父项目 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/>
</parent>
<!-- 项目坐标 -->
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<!-- 项目信息 -->
<name>my-app</name>
<description>Demo project</description>
<!-- 属性配置 -->
<properties>
<java.version>17</java.version>
</properties>
<!-- 依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- 构建插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
关键配置说明
| 配置 | 作用 |
|---|---|
| spring-boot-starter-parent | 继承默认配置和版本管理 |
| spring-boot-starter-web | Web开发依赖集合 |
| spring-boot-maven-plugin | 打包可执行JAR插件 |
starter-parent作用
XML
<!-- 继承后自动获得:
1. 依赖版本管理
2. 默认插件配置
3. 属性默认值
-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
</parent>
启动类解析
主类代码
Java
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@SpringBootApplication组成
Java
@SpringBootApplication = @SpringBootConfiguration // 配置类
+ @EnableAutoConfiguration // 自动配置
+ @ComponentScan // 组件扫描
| 注解 | 作用 |
|---|---|
| @SpringBootConfiguration | 标记为配置类 |
| @EnableAutoConfiguration | 启用自动配置 |
| @ComponentScan | 扫描当前包及子包 |
SpringApplication.run()
Java
// 启动流程
SpringApplication.run(MyApplication.class, args)
→ 创建SpringApplication实例
→ 准备环境
→ 创建ApplicationContext
→ 加载Bean定义
→ 刷新容器
→ 启动内嵌容器
→ 应用就绪
配置文件解析
application.properties
properties
server.port=8080
spring.application.name=my-app
spring.profiles.active=dev
application.yml
YAML
server:
port: 8080
spring:
application:
name: my-app
profiles:
active: dev
配置文件作用
| 配置项 | 作用 |
|---|---|
| server.port | 服务端口 |
| spring.application.name | 应用名称 |
| spring.profiles.active | 激活环境 |
目录结构说明
text
src/
├── main/
│ ├── java/ # Java源代码
│ │ └── com/example/
│ │ └── MyApplication.java
│ └── resources/ # 资源文件
│ ├── application.yml # 配置文件
│ ├── static/ # 静态资源
│ └── templates/ # 模板文件
└── test/ # 测试代码
└── java/
| 目录 | 作用 |
|---|---|
| src/main/java | 源代码目录 |
| src/main/resources | 资源目录 |
| static | 静态文件(CSS/JS/图片) |
| templates | 模板文件 |
默认资源位置
| 资源 | 默认路径 |
|---|---|
| 静态资源 | classpath:/static/ |
| 模板文件 | classpath:/templates/ |
| 配置文件 | classpath:/application.yml |
启动类位置规范
text
# ✅ 正确位置:根包
com/example/
├── MyApplication.java # 启动类
├── controller/ # 自动扫描
├── service/ # 自动扫描
# ❌ 错误位置
com/
├── example/
│ └── controller/
├── MyApplication.java # 不扫描controller包
启动类放在根包下,@ComponentScan 自动扫描子包组件。
要点总结
- pom.xml继承starter-parent简化配置
- @SpringBootApplication组合三个核心注解
- SpringApplication.run()启动容器
- application.yml是主配置文件
- 启动类放根包自动扫描子包
📝 发现内容有误?点击此处直接编辑