Springboot 的Servlet Web 应用、响应式 Web 应用(Reactive)以及非 Web 应用(None)的特点和适用场景

基于 Servlet 的 Web 应用 (Servlet Web)

特点

使用传统的 Servlet API 和 Spring MVC 框架。

采用阻塞 I/O 模型,每个请求都会占用一个线程直到请求处理完毕。

适合处理同步请求-响应模式的应用。

依赖

spring-boot-starter-web:这是核心依赖,它会自动引入 Tomcat 作为默认的嵌入式服务器。也可以通过排除默认的 Tomcat 依赖并添加 Jetty 或 Undertow 依赖来更换服务器。

复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
主要技术栈

Spring MVC:用于处理 HTTP 请求和响应。

Thymeleaf, JSP, FreeMarker:用于模板引擎,生成 HTML 页面。

Jackson:用于 JSON 处理。

Tomcat, Jetty, Undertow:嵌入式 Web 服务器。

应用场景

传统的 Web 应用。

RESTful API 服务。

表单提交处理。

文件上传下载。

示例代码
复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class ServletWebApplication {

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

    @RestController
    public static class HelloController {

        @GetMapping("/hello")
        public String hello() {
            return "Hello, Servlet Web!";
        }
    }
}

响应式 Web 应用 (Reactive Web)

特点

基于响应式编程模型,使用非阻塞 I/O 模型。

适用于高并发和大数据流处理的应用。

使用 Spring WebFlux 框架,支持异步和非阻塞处理。

依赖

spring-boot-starter-webflux:这是核心依赖,它会自动引入 Netty 作为默认的嵌入式服务器。也可以使用 Tomcat 或 Undertow 作为服务器,但通常 Netty 是更好的选择。

复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
主要技术栈

Spring WebFlux:用于处理 HTTP 请求和响应。

Reactor:响应式编程库,用于异步和非阻塞处理。

Netty, Tomcat, Undertow:嵌入式 Web 服务器,Netty 是默认选择。

RSocket:用于构建响应式微服务。

应用场景

高并发处理,例如聊天应用、实时数据处理。

微服务架构中的服务。

实时数据流处理。

低延迟响应要求高的应用。

示例代码
复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.server.RequestPredicates;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

@SpringBootApplication
public class ReactiveWebApplication {

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

    @Bean
    public RouterFunction<ServerResponse> routes() {
        return route(GET("/hello"), request -> ServerResponse.ok().bodyValue("Hello, Reactive Web!"));
    }
}

非 Web 应用 (None)

特点

不包含 Web 服务器,适用于后台处理、定时任务、批处理作业等不需要 Web 服务器的应用。

可以使用 Spring 的各种功能,如依赖注入、AOP、事务管理等。

依赖

不需要引入 spring-boot-starter-webspring-boot-starter-webflux

根据需求引入其他必要的依赖,如 spring-boot-starter-data-jpaspring-boot-starter-quartz 等。

复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
主要技术栈

Spring Core:核心框架,提供依赖注入、AOP 等功能。

Spring Data JPA:简化数据库访问。

Quartz:用于定时任务调度。

Spring Batch:用于批处理作业。

应用场景

后台任务调度。

数据处理和批处理作业。

命令行工具。

定时任务。

示例代码
复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@SpringBootApplication
@EnableScheduling
public class NonWebApplication {

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

    @Scheduled(fixedRate = ½000)
    public void performTask() {
        System.out.println("Executing scheduled task...");
    }
}

总结

Servlet Web 适用于传统的 Web 应用和 RESTful 服务。

Reactive Web 适用于高并发和低延迟要求的应用。

Non Web 适用于后台处理、定时任务和批处理作业。

选择合适的应用类型取决于具体的需求和技术栈偏好。

相关推荐
invicinble1 分钟前
jar包在执行的时候需要关注的细节(提供一个解构jvm问题的视角)
java·jvm·jar
麦芽糖02193 分钟前
SSE介绍及使用(Server-Send Events)
java
alan07217 分钟前
【Java + Elasticsearch全量 & 增量同步实战】
java·elasticsearch·jenkins
Rover.x20 分钟前
Netty基于SpringBoot实现WebSocket
spring boot·后端·websocket
hashiqimiya20 分钟前
后端springboot的接收前端发来的数据反序列化原理
java
cat三三1 小时前
java之异常
java·开发语言
浙江第二深情1 小时前
前端性能优化终极指南
java·maven
养乐多07221 小时前
【Java】IO流
java
俊男无期1 小时前
超效率工作法
java·前端·数据库
中国胖子风清扬1 小时前
SpringAI和 Langchain4j等 AI 框架之间的差异和开发经验
java·数据库·人工智能·spring boot·spring cloud·ai·langchain