SpringBoot应用程序Tomcat部署

我有一个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文件夹中.但是部署发生了两次,并且因为上下文已经存在而以异常结束.我错过了什么?

非常感谢帮助.

最佳答案
您应该添加一个main方法.

看看这些例子: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 / Catalina /”中的每个应用程序上下文路径放置一个自定义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的专家,但你的依赖关系看起来很好.

希望有所帮助.

转载注明原文:SpringBoot应用程序Tomcat部署 - 代码日志