1.《SpringBoot实战》学习笔记-第1章
2.《SpringBoot实战》学习笔记-第2章
第2章
1. 创建SpringBoot工程
(略)使用Idea创建SpringBoo工程
2. 默认SpringBoot工程结构
- pom.xml :maven工程文件
- ReadingListApplication.java: 应用程序的启动引导类,也是主要的Spring配置类
- application.properties: 用于配置应用程序和Spring Boot的属性
- ReadingListApplicationTest.java:一个基本的集成测试类
3.要点说明
3.1 启动引导Spring
ReadingListApplication类既是配置类也是启动引导类,如下所示:
package com.example.readinglist;
点击查看代码
package com.example.readinglist;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ReadinglistApplication {
public static void main(String[] args) {
SpringApplication.run(ReadinglistApplication.class, args);
}
}
其中@SpringBootApplication注解将三个注解组合在一起:
- Spring的@Configuration:标明该类使用Spring基于Java的配置
- Spring的@ComponentScan:启用组件扫描
- SrpingBoot的@EnableAutoConfiguration:开启SpringBoot自动配置功能。
3.2 测试SpringBoot用用程序
初始化脚手架自动增加了测试类的骨架,代码如下:
点击查看代码
package com.example.readinglist;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ReadinglistApplicationTests {
@Test
void contextLoads() {
}
}
这里需要补充
3.3 配置应用程序属性
初始化脚手架会自动生成application.properties配置文件,这个文件虽然是可选的,但是在真正的工程中,基本是必用的,而且有更好的用法。
3.4 项目构建过程
这里用的是maven做为构建工具,如果使用Gradle,则参考Gradle。
点击查看代码
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>readinglist</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>readinglist</name>
<description>Reading List project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这里需要补充maven构建插件的相关内容
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~