一尘不染

如何使用Tomcat 8 + Spring Boot + Maven

spring-boot

根据“ 使用Tomcat 8的参考API” 以及本“
部署Spring Boot应用程序”教程,应该可以将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);
    }
}

输出是 启动Servlet引擎: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)

我已经尝试过这个较新的RC版本的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

阅读 244

收藏
2020-05-30

共1个答案

一尘不染

当使用Spring Boot重写tomcat tomcat.version作为属性时,仅当您spring-boot-starter- parent用作项目的父项时才起作用。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.0.M2</version>
</parent>

(将版本更改为您喜欢的版本)。

<properties>
    <tomcat.version>8.0.8</tomcat.version>
</properties>

如果您不想将其用作父项,则必须<dependencyManagement>在pom.xml中使用一个部分来覆盖所有spring版本,并保留上面的属性。

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-el</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-logging-juli</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-websocket</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jsp-api</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
    </dependencies>

</dependencyManagement>

无论哪种方法都可以,但是扩展父级是最简单的方法,但这并不总是可行的。

2020-05-30