Spring Boot 作為 Spring 生態系的「意見化」框架,以「約定優於配置」重新定義了 Java 企業級開發體驗。本文從核心理念切入,手把手帶你從零構建一個可部署的 Spring Boot Web 應用。
spring-boot-starter-* 聚合常用依賴,解決版本衝突@EnableAutoConfiguration 根據類路徑自動裝配 Beansrc/ ├── main/ │ ├── java/ │ │ └── com/example/ │ │ ├── Application.java # 啟動類 │ │ ├── config/ # 配置類 │ │ ├── controller/ # 控制層 │ │ ├── service/ # 業務層 │ │ ├── repository/ # 資料層 │ │ └── entity/ # 實體類 │ └── resources/ │ ├── application.yml # 主配置 │ ├── application-dev.yml # 開發環境 │ ├── application-prod.yml # 生產環境 │ └── static/templates/ # 靜態資源/模板 └── test/ # 測試代碼
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/>
</parent>
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 熱部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
@SpringBootApplication
@EnableAsync
public class Application implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
factory.setPort(8087); // 自定義端口
factory.setContextPath("/api"); // 上下文路徑
factory.setSessionTimeout(Duration.ofMinutes(30));
}
}
IDE 熱部署配置
IntelliJ IDEA 需啟用「Build project automatically」與「Allow auto-make module even when application is running」:
- Settings → Build, Execution, Deployment → Compiler → 勾選 Build project automatically
- Ctrl+Shift+A → Registry → 勾選 compiler.automake.allow.when.app.running
- 專案加入
spring-boot-devtools依賴多環境配置與打包部署
# application-prod.yml server: port: 80 servlet: context-path: / spring: profiles: active: prod datasource: url: jdbc:mysql://prod-db:3306/app?useSSL=false hikari: maximum-pool-size: 50打包:
mvn clean package -DskipTests→ 生成target/app.jar部署:
java -jar app.jar --spring.profiles.active=prod常見問題排查
| 現象 | 原因 | 解決 |
|---|---|---|
| 端口衝突 | 預設 8080 被佔用 | 配置 server.port 或 factory.setPort() |
| 靜態資源 404 | 路徑不在預設目錄 | 資源放置 src/main/resources/static 或 public/resources/META-INF/resources |
| 熱部署無效 | IDE 未啟用自動編譯 | 按上述步驟開啟 IDE 自動編譯 |
| 事務不生效 | 方法為 private/同類調用 | 方法改 public、拆分 Bean 或使用 @EnableAspectJAutoProxy(exposeProxy=true) |
Spring Boot 透過自動配置、起步依賴、內嵌容器三大支柱,將傳統 Spring 應用的啟動時間從「分鐘級」壓縮至「秒級」,配置量減少 80% 以上。掌握其核心機制與最佳實踐,是現代 Java 開發者的必備技能。