SPRING BOOT 手动 War 文件部署到 Tomcat 并重新启动服务器,不创建数据库

Posted

技术标签:

【中文标题】SPRING BOOT 手动 War 文件部署到 Tomcat 并重新启动服务器,不创建数据库【英文标题】:SPRING BOOT Manual War file Deployment to Tomcat and restarting server, Not create Database 【发布时间】:2015-05-21 11:21:59 【问题描述】:

当我从 IDE 运行我的 spring boot 项目时,它会创建数据库并将战争部署到 tomcat。当我将war文件手动部署到tomcat并重新启动tomcat时,我希望看到同样的事情。但似乎在部署期间没有创建数据库。我在这里想念什么。

我记得,在过去,我以前做过同样的事情并且它有效,是与Spring Boot有关还是与pom文件中提供的构建插件有问题

这是我的 POM 文件。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xxx.yyy</groupId>
    <artifactId>webapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.5.RELEASE</version>
        <relativePath />
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> 
            </dependency> -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-solr</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-integration</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>$jstl.version</version>
        </dependency>

        <dependency>
            <groupId>displaytag</groupId>
            <artifactId>displaytag</artifactId>
            <version>$displaytag.version</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>com.xxx.Application</start-class>
        <java.version>1.7</java.version>
        <jstl.version>1.2</jstl.version>
        <displaytag.version>1.2</displaytag.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

【问题讨论】:

【参考方案1】:

我想通了。

我将 Application 类更改为

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application  extends SpringBootServletInitializer

    public static void main(String[] args) 
        SpringApplication.run(applicationClass, args);
    

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) 
        return application.sources(applicationClass);
    

     private static Class<Application> applicationClass = Application.class;



并删除了以下插件

<!-- <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin> -->

并添加了

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

它有效:)。万事如意

【讨论】:

以上是关于SPRING BOOT 手动 War 文件部署到 Tomcat 并重新启动服务器,不创建数据库的主要内容,如果未能解决你的问题,请参考以下文章

将 WAR 部署到 Tomcat(Spring Boot + Angular)

spring boot --部署war到tomcat中

spring boot项目打成war包部署到tomcat

spring boot怎么部署到服务器

spring boot 部署war 到tomcat上面静态资源无法访问

一个 Spring Boot 项目,部署到 JAR 或 WAR