2015-10-29 173 views
5

我在学习Spring引导,我注意到有两个选项。spring-boot-starter-tomcat vs spring-boot-starter-web

  1. 弹簧引导启动的Web - 根据文档这给支持全堆栈的Web开发,包括Tomcat和Web的MVC

  2. 弹簧引导起动tomcat的

因为#1支持Tomcat为什么要使用#2?

有什么区别?

感谢

回答

6

由于#1支持Tomcat的一个为什么要使用#2?

spring-boot-starter-web包含spring-boot-starter-tomcatspring-boot-starter-tomcat如果不需要弹簧mvc,可能会自行使用(包含在spring-boot-starter-web中)。

这里是spring-boot-starter-web依赖层次:

enter image description here

有什么区别?

spring-boot-starter-web包含春天网络依赖(包括spring-boot-starter-tomcat):

spring-boot-starter
jackson
spring-core
spring-mvc
spring-boot-starter-tomcat

spring-boot-starter-tomcat包含与一个embdedded tomcat的一切服务器:

core
el
logging
websocket

如果你想使用Spring MVC中不包括嵌入的Tomcat服务器?

就排除它从依赖关系:

<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
     <exclusions> 
      <exclusion> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-starter-tomcat</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency> 
2

那么一个简单的答案是,并非所有的Web应用程序是应用程序用SpringMVC。例如,如果您希望使用JaxRS,也许您有使用RestTemplate的客户端应用程序,并且您喜欢它们之间的交互方式,但这并不意味着您无法使用弹簧引导或嵌入式tomcat。

这是一个示例应用程序,它使用spring-boot-starter-tomcat但不spring-boot-starter-web

使用在春季启动的简单应用新泽西spring-boot-starter-tomcat

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-jersey

还应该记住,tomcat并不是Spring引导中嵌入式servlet容器的唯一选项。开始使用码头也很容易。并具有spring-boot-starter-tomcat可以很容易地全部排除作为一个模块,而如果他们弹簧网这将是更多的工作,排除一切只是部分tomcat的库在spring-boot-starter-jersey带来代替

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-web</artifactId> 
    <exclusions> 
     <exclusion> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-tomcat</artifactId> 
     </exclusion> 
    </exclusions> 
</dependency> 
<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-jetty</artifactId> 
</dependency> 

我从复制这个代码另一个SO问题在这里。

How to configure Jetty in spring-boot (easily?)

相关问题