知識庫 / Spring RSS 訂閱

使用 Spring 構建 Web 應用程序

REST,Spring
HongKong
4
04:11 AM · Dec 06 ,2025

1. 概述

本教程將演示如何使用 Spring 創建 Web 應用程序

我們將探討 Spring Boot 解決方案構建應用程序,並查看非 Spring Boot 方法。

我們將主要使用 Java 配置,同時也會查看其 XML 配置的等效方法。

2. 使用 Spring Boot 設置

Spring Boot 是一種用於快速構建生產就緒 Spring 應用的框架。它簡化了 Spring 應用的配置過程,並提供了自動配置和外部配置等功能。

安裝 Spring Boot

  1. 下載 Spring Boot: 從 Spring.io 下載最新的 Spring Boot 軟件包。
  2. 解壓軟件包: 將下載的 .jar 文件解壓到您的項目目錄中。
  3. 運行 Spring Boot 應用: 使用 java -jar your-app.jar 命令運行 Spring Boot 應用。

配置 Spring Boot 應用

Spring Boot 應用的配置可以通過以下方式進行:

  • 自動配置: Spring Boot 會自動配置一些常用的組件,例如數據源、消息隊列等。
  • 外部配置: 您可以自定義 Spring Boot 應用的配置,例如數據庫連接信息、緩存配置等。
    • YAML 配置文件: Spring Boot 推薦使用 YAML 配置文件來配置外部配置。
    • 命令行參數: 您也可以使用命令行參數來配置 Spring Boot 應用。

2.1. Maven 依賴

首先,我們需要 spring-boot-starter-web 依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

此啓動器包含:

  • spring-webspring-webmvc 模塊,用於我們的 Spring Web 應用程序
  • Tomcat 啓動器,以便我們可以在不顯式安裝任何服務器的情況下直接運行 Web 應用程序

2.2. 創建 Spring Boot 應用程序

使用 Spring Boot 的最簡單方法是創建主類並使用 @SpringBootApplication 註解進行標註:

@SpringBootApplication
public class SpringBootRestApplication {

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

此單個註釋相當於使用 @Configuration@EnableAutoConfiguration@ComponentScan

默認情況下,它會掃描同一包及其子包中的所有組件。

接下來,對於基於 Java 的 Spring Bean 配置,我們需要創建一個配置類並使用 @Configuration 註解進行標註:

@Configuration
public class WebConfig {

}

此註釋是基於 Java 的 Spring 配置的主要元數據;它本身又被 @Component 標註,從而使標註的類成為標準 Bean,並因此也成為掃描候選對象。

@Configuration 類的主要目的是為 Spring IoC 容器提供 Bean 定義的來源。有關更詳細的描述,請參閲 官方文檔

讓我們也看看使用核心 spring-webmvc 庫的解決方案。

3. 使用 spring-webmvc 設置

// 設置 Spring Web MVC 環境
// Configure the Spring Web MVC environment

3.1. Maven 依賴

首先,我們需要 spring-webmvc 依賴:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.3</version>
</dependency>

3.2. 基於Java的Web配置

接下來,我們將添加具有@Configuration註解的配置類:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.baeldung.controller")
public class WebConfig {
   
}

與 Spring Boot 解決方案不同,這裏我們需要顯式地定義 @EnableWebMvc 以設置默認的 Spring MVC 配置,以及 @ComponentScan 以指定掃描組件的包。

@EnableWebMvc 註解提供 Spring Web MVC 配置,例如設置 Dispatcher Servlet、啓用 @Controller@RequestMapping 註解以及設置其他默認配置。

@ComponentScan 配置組件掃描指令,指定掃描的包。

3.3. 初始化類

接下來,我們需要添加一個實現 WebApplicationInitializer 接口的類:

public class AppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.scan("com.baeldung");
        container.addListener(new ContextLoaderListener(context));

        ServletRegistration.Dynamic dispatcher = 
          container.addServlet("mvc", new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");   
    }
}

在這裏,我們使用 AnnotationConfigWebApplicationContext 類創建 Spring 容器,這意味着我們僅使用基於註解的配置。然後,我們指定掃描組件和配置類所在的包。

最後,我們定義了 Web 應用程序的入口點——DispatcherServlet

該類可以完全替代 <3.0 Servlet 版本中的 web.xml 文件。

4. XML 配置文件

讓我們快速看一下等效的 XML Web 配置:

<context:component-scan base-package="com.baeldung.controller" />
<mvc:annotation-driven />

我們可以用上面的 WebConfig 類替換這個 XML 文件。

要啓動應用程序,我們可以使用一個 Initializer 類來加載 XML 配置或 web.xml 文件。有關這兩種方法的更多詳細信息,請查看我們之前的文章。

5. 結論在本文中,我們探討了兩種流行的 Spring Web 應用程序的啓動方案,一種使用 Spring Boot Web Starter,另一種使用核心 spring-webmvc 庫。

在下一篇關於 Spring 中 REST 的文章中,我將涵蓋在項目中設置 MVC、HTTP 狀態碼的配置、負載映射以及內容協商。

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.