首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Spring Boot 進階實戰:標準項目結構與核心組件開發詳解

     編輯:Java 綜合教學

Spring Boot 進階實戰:標準項目結構與核心組件開發詳解

承接上一節 Spring Boot 入門程序,本文將深入介紹官方推薦的標準開發模式,並構建包含 Controller、Service、Domain 層的完整應用結構,展示配置屬性綁定、依賴注入等核心機制的實際應用。

一、標準項目目錄結構

遵循官方建議,將主啟動類置於根包路徑(如 com.xxx.firstboot)下,以便 @ComponentScan 自動掃描子包。典型分層結構如下:

  • domain:實體類,配合 @ConfigurationProperties 實現配置自動綁定
  • service:業務邏輯層,通過 @Service 標識,注入 Domain 對象
  • web:控制層,使用 @RestController 暴露 RESTful API

項目結構圖

二、Maven 核心配置 (pom.xml)

採用 spring-boot-starter-parent 作為父項目,統一管理依賴版本;引入 spring-boot-starter-web 提供 Web 開發所需的 Spring MVC、Tomcat、Jackson 等組件;配置 spring-boot-maven-plugin 支援打包為可執行 JAR。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
</parent>
<properties>
    <java.version>1.8</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>

注意:若無法繼承官方父項目,可改用 dependencyManagement 導入 spring-boot-dependencies,並手動配置打包插件。

三、啟動類與核心註解解析

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@SpringBootApplication 組合了三大核心註解:

  • @Configuration:標識配置類,由 Spring 容器管理
  • @EnableAutoConfiguration:啟用自動配置機制,根據類路徑依賴自動裝配 Bean
  • @ComponentScan:掃描主類所在包及其子包(主類置於根包的關鍵原因)

每個獨立 JAR(微服務)僅允許存在一個 main 方法作為啟動入口。

四、配置文件與屬性綁定

application.properties 是 Spring Boot 默認配置文件,集中管理數據源、HTTP 客戶端等各類配置:

user.id=1
user.username=zhaojigang
user.password=123

在 Domain 類上使用 @Component + @ConfigurationProperties(prefix="user") 即可實現屬性自動注入:

@Component
@ConfigurationProperties(prefix="user")
public class User {
    private int id;
    private String username;
    private String password;
    // getter/setter 省略
}

此方式比逐個使用 @Value("${user.id}") 更優雅;若同時存在 @ConfigurationProperties,則 @Value 失效。

五、Service 層與 Controller 層實現

UserService 注入 User 實體,提供業務方法:

@Service
public class UserService {
    @Autowired
    private User user;
    public User getUser() { return user; }
}

UserController 使用 @RestController(等同於 @Controller + @ResponseBody)直接返回 JSON:

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getUser")
    public User getUser() {
        return userService.getUser();
    }
}

此即為 Spring Boot 開發中最基礎、最常用的標準模式(微服務多模塊結構後續介紹)。

六、屬性文件讀取方式總結

  1. 註解綁定(推薦):@ConfigurationProperties / @Value
  2. Environment 對象:注入 Environment 通過 getProperty() 獲取
  3. 工具類讀取:傳統 FileUtil 方式(不推薦)

七、常用 Starter 依賴一覽

Spring Boot 提供大量開箱即用的 Starter,按需引入:

  • Web 開發:spring-boot-starter-web
  • 數據訪問:spring-boot-starter-data-jpa、spring-boot-starter-jdbc
  • 安全認證:spring-boot-starter-security
  • 模板引擎:spring-boot-starter-thymeleaf、spring-boot-starter-freemarker
  • 消息隊列:spring-boot-starter-amqp、spring-boot-starter-activemq
  • 監控運維:spring-boot-starter-actuator

Starter列表1 Starter列表2 Starter列表3 Starter列表4 Starter列表5

提示:當前版本暫無官方 MyBatis Starter,需手動整合或使用第三方 Starter。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved