欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > Springboot 的Servlet Web 应用、响应式 Web 应用(Reactive)以及非 Web 应用(None)的特点和适用场景

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

2025/5/2 16:36:21 来源:https://blog.csdn.net/m0_50742275/article/details/143657675  浏览:    关键词: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);}@RestControllerpublic 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);}@Beanpublic RouterFunction<ServerResponse> routes() {return route(GET("/hello"), request -> ServerResponse.ok().bodyValue("Hello, Reactive Web!"));}
}

非 Web 应用 (None)

         特点

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

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

         依赖

                  不需要引入 spring-boot-starter-web 或 spring-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 适用于后台处理、定时任务和批处理作业。

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词