我刚刚开始学习春天。在我的下一步,我想开发更大的网络应用程序。

现在我想知道我是否应该从Spring Boot或Spring MVC开始。我已经读了一些东西,但这是令人困惑的,因为两者看起来相似。

那么两者之间有什么不同呢?


当前回答

Spring MVC is a complete HTTP oriented MVC framework managed by the Spring Framework and based in Servlets. It would be equivalent to JSF in the JavaEE stack. The most popular elements in it are classes annotated with @Controller, where you implement methods you can access using different HTTP requests. It has an equivalent @RestController to implement REST-based APIs. Spring boot is a utility for setting up applications quickly, offering an out of the box configuration in order to build Spring-powered applications. As you may know, Spring integrates a wide range of different modules under its umbrella, as spring-core, spring-data, spring-web (which includes Spring MVC, by the way) and so on. With this tool you can tell Spring how many of them to use and you'll get a fast setup for them (you are allowed to change it by yourself later on).

因此,Spring MVC是一个用于web应用程序的框架,Spring Boot是一个基于Spring的生产就绪项目初始化器。您可能会发现访问Spring MVC标记wiki以及SO中的Spring Boot标记wiki很有用。

其他回答

SpringBoot实际上是预配置的,减少了锅炉配置,并提供了最简单或快速的方式来启动应用程序。

SpringBoot将令人头痛的配置问题从开发人员转移到它自己,而不是Spring。

隐式SpringBoot基于Spring框架概念,如bean、控制器、服务、jpa等。

可以说SpringBoot是Spring的包装器。

在SpringBoot中,服务器的默认端口是8080,但如果你想改变,那就去你的应用程序。属性和写入

server.port = 8084

Spring MVC is a complete HTTP oriented MVC framework managed by the Spring Framework and based in Servlets. It would be equivalent to JSF in the JavaEE stack. The most popular elements in it are classes annotated with @Controller, where you implement methods you can access using different HTTP requests. It has an equivalent @RestController to implement REST-based APIs. Spring boot is a utility for setting up applications quickly, offering an out of the box configuration in order to build Spring-powered applications. As you may know, Spring integrates a wide range of different modules under its umbrella, as spring-core, spring-data, spring-web (which includes Spring MVC, by the way) and so on. With this tool you can tell Spring how many of them to use and you'll get a fast setup for them (you are allowed to change it by yourself later on).

因此,Spring MVC是一个用于web应用程序的框架,Spring Boot是一个基于Spring的生产就绪项目初始化器。您可能会发现访问Spring MVC标记wiki以及SO中的Spring Boot标记wiki很有用。

以下是区分Spring、Spring MVC和Spring Boot的一些要点:

春天:

Main Difference is "Test-ability". Spring come with the DI and IOC. Through which all hard-work done by system we don't need to do any kind of work(like, normally we define object of class manually but through Di we just annotate with @Service or @Component - matching class manage those). Through @Autowired annotation we easily mock() it at unit testing time. Duplication and Plumbing code. In JDBC we writing same code multiple time to perform any kind of database operation Spring solve that issue through Hibernate and ORM. Good Integration with other frameworks. Like Hibernate, ORM, Junit & Mockito.

Spring MVC

Spring MVC框架是Spring的一个模块,它为面向HTTP的web应用开发提供了便利。 Spring MVC在输入逻辑(控制器)、业务逻辑(模型)和UI逻辑(视图)上有明确的代码分离。 Spring MVC模式有助于开发灵活和松散耦合的web应用程序。 根据您的需要,提供各种硬编码方式来定制您的应用程序。

Spring Boot:

Create of Quick Application so that, instead of manage single big web application we divide them individually into different Microservices which have their own scope & capability. Auto Configuration using Web Jar : In normal Spring there is lot of configuration like DispatcherServlet, Component Scan, View Resolver, Web Jar, XMLs. (For example if I would like to configure datasource, Entity Manager Transaction Manager Factory). Configure automatically when it's not available using class-path. Comes with Default Spring Starters, which come with some default Spring configuration dependency (like Spring Core, Web-MVC, Jackson, Tomcat, Validation, Data Binding, Logging). Don't worry about versioning issue as well.

(演化类似:Spring -> Spring MVC -> Spring Boot,所以新版本兼容旧版功能。) 注:它不包含所有点。

Spring MVC和Spring Boot存在的目的不同。因此,将彼此作为竞争者进行比较是不明智的。

什么是Spring Boot?

Spring Boot是一个用合理的默认值打包Spring应用程序的框架。这是什么意思?你正在使用Spring MVC, Spring Data, Hibernate和Tomcat开发一个web应用程序。如何将此应用程序打包并部署到web服务器。到目前为止,我们必须手动编写配置,XML文件等部署到web服务器。

Spring Boot通过项目中的Zero XML配置为您完成这一任务。相信我,你不需要部署描述符,web服务器等。Spring Boot是一个神奇的框架,它为您捆绑了所有依赖项。最后,您的web应用程序将是一个带有嵌入式服务器的独立JAR文件。

如果你仍然不明白这是如何工作的,请阅读关于使用spring引导开发微服务框架的文章。

什么是Spring MVC?

它是一个传统的web应用程序框架,帮助您构建web应用程序。它类似于Struts框架。

Spring MVC是一个用于构建web应用程序的Java框架。它遵循模型-视图-控制器设计模式。它实现了核心spring框架的所有基本特性,如控制反转、依赖注入。

Spring MVC借助DispatcherServlet为在Spring框架中使用MVC提供了一种优雅的解决方案。在这里,DispatcherServlet是一个接收传入请求并将其映射到正确资源(如控制器、模型和视图)的类。

我希望这能帮助您理解其中的区别。

Spring MVC和Spring Boot在其他答案中有很好的描述,所以不再重复,让我直接跳到细节。Spring Boot和Spring MVC并不具有可比性或互斥性。如果你想使用Spring进行web应用程序开发,无论如何你都会使用Spring MVC。接下来的问题是是否使用Spring Boot。

对于开发普通Spring应用程序或开始学习Spring,我认为建议使用Spring Boot。它极大地减轻了工作,已投入生产,并迅速被广泛采用。

我有时会看到初学者问这个问题,因为在STS (Spring Tool Suite)中有两个向导:一个用于创建Spring Boot项目,另一个用于创建Spring MVC项目。因此,我的建议是创建一个Spring Boot项目,并选择Web作为其中的一个模块。