SpringBoot应用程序Tomcat部署

时间:2017-12-07 05:54:37

标签: tomcat spring-boot

我有一个SpringBoot应用程序,我正在尝试部署到Tomcat服务器。根据在线参考资料,我在Application类中添加了一些代码,如下所示:

public class SkyVetApplication extends SpringBootServletInitializer{
...
   @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(SkyVetApplication.class);
}
...
}

build.gradle我添加了以下内容:

compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
**providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'**

执行干净构建后,我将war文件复制到Tomcat的webapps文件夹。但是部署发生了两次,并且因为上下文已经存在而以异常结束。我错过了什么?

非常感谢帮助。

1 个答案:

答案 0 :(得分:0)

您应该添加一个主要方法。

查看以下示例:https://github.com/Pytry/bootiful-war-deployment

以下是“hello”模块的示例(它使用的是Lombok注释处理器)。

package com.example.bootifulwar;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@SpringBootApplication
@EnableScheduling
@Slf4j
public class HelloServletInitializer extends SpringBootServletInitializer{

    @Value("${messageForUser}")
    private String message;

    @Value("${whatDoesTheFoxSay:'No body knows.'}")
    private String whatDoesTheFoxSay;

    public static void main(String[] args){

        SpringApplication.run(HelloServletInitializer.class, args);
    }

    @Scheduled(fixedRate = 2000)
    public void sayHelloTo(){

        log.info("Hello! " + message);
    }

    @Override
    public SpringApplicationBuilder configure(SpringApplicationBuilder application){

        log.info(
            "\n*********************\n" +
                "What does the fox say?\n" +
                whatDoesTheFoxSay +
                "\n*********************\n");
        return application.sources(HelloServletInitializer.class);
    }
}

要利用个性化日志记录和外部“application.properties”,假设您将多个war文件部署到同一个Tomcat,则需要为“conf”中的每个应用程序上下文路径放置一个自定义context.xml。 /卡塔利娜/本地主机。“

示例:

<?xml version='1.0' encoding='utf-8'?>
<Context docBase="hello.war" path="hello">
  <Resources className="org.apache.catalina.webresources.StandardRoot">
    <PreResources base="hello\\config"
                  className="org.apache.catalina.webresources.DirResourceSet"
                  internalPath="/"
                  webAppMount="/WEB-INF/classes"/>
  </Resources>
</Context>

我不是gradlem的专家,但你的依赖关系看起来很好。

希望有所帮助。

相关问题