根据Using Tomcat 8上的参考API和这个Deploy Spring Boot Applications教程,应该可以将Tomcat 8与Spring Boot一起使用,它使用Tomcat 7作为默认值 .
出于某种原因,它不适合我 . 我究竟做错了什么?
pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<tomcat.version>8.0.3</tomcat.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
</dependencies>
SampleController.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
Output is Starting Servlet Engine: Apache Tomcat/7.0.52
:: Spring Boot :: (v1.0.2.RELEASE)
2014-06-09 13:55:27.688 INFO 5744 --- [ main] SampleController : Starting SampleController on CI01081252 with PID 5744 (started by TECBMEPI in D:\projetos\teclogica\testes automatizados\exemplo int db\workspace\spring-boot-tomcat8-maven)
2014-06-09 13:55:27.730 INFO 5744 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@33b45e9a: startup date [Mon Jun 09 13:55:27 BRT 2014]; root of context hierarchy
2014-06-09 13:55:28.869 INFO 5744 --- [ main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8081
2014-06-09 13:55:29.117 INFO 5744 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2014-06-09 13:55:29.118 INFO 5744 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.52
2014-06-09 13:55:29.226 INFO 5744 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2014-06-09 13:55:29.226 INFO 5744 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1498 ms
2014-06-09 13:55:29.656 INFO 5744 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2014-06-09 13:55:29.658 INFO 5744 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2014-06-09 13:55:29.946 INFO 5744 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-06-09 13:55:30.035 INFO 5744 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto java.lang.String SampleController.home()
2014-06-09 13:55:30.059 INFO 5744 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-06-09 13:55:30.059 INFO 5744 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-06-09 13:55:30.236 INFO 5744 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2014-06-09 13:55:30.257 INFO 5744 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081/http
2014-06-09 13:55:30.259 INFO 5744 --- [ main] SampleController : Started SampleController in 2.933 seconds (JVM running for 3.318)
我已经尝试过这个newer RC version of Spring boot,但仍然加载Tomcat 7而不是8 .
添加 <java.version>1.7</java.version>
属性也没有用 .
尝试了“application.properties” . 端口在启动时设置为指定,但Tomcat的版本仍为默认值7 .
application.properties
server.port=8081
tomcat.version=8.0.3
3 回答
使用Spring Boot覆盖tomcat并使用
tomcat.version
作为属性时,只有在使用spring-boot-starter-parent
作为项目的父级时才能使用 .(将版本更改为您喜欢的版本) .
如果您不想将其用作父级,则必须使用pom.xml中的
<dependencyManagement>
部分覆盖所有spring版本并保留上述属性 .无论哪种方式都可行,但延伸父母是我最容易说的,但这并不总是可行的 .
你try overriding tomcat.version in your application.properties?具体来说,将其设置为8.0.3会使spring boot运行8.0.3而不是默认值 .
我可以像回答another thread一样回答这个问题 . 我认为有些人不知道Spring Boot的一个关键信息只是模拟实际的Java Servlet容器,如Jetty,Glassfish,Tomcat等 . 您作为开发人员必须选择您将使用的 .
Spring Boot是不错的,但当然有它的垮台,并且实际上只是由spring.io团队创建,以使他们的教程看起来更好,以试图与其他'在几秒钟内启动和运行'技术竞争 .