部署在tomcat上的Spring Boot无法启动

Posted

技术标签:

【中文标题】部署在tomcat上的Spring Boot无法启动【英文标题】:Spring boot deployed on tomcat won't start 【发布时间】:2016-10-21 12:52:12 【问题描述】:

我有一个包含嵌入式 Tomcat 的有效 Spring 应用程序。作为一个可执行的战争,它运行良好。由于我将使用 AngularJ 作为前端,因此将 Spring 项目放在可执行 jar 中并不是很实用,因为我将在浏览器中进行调试并希望能够快速编辑 js 源文件。如果将它们保存在存档中,这很烦人。

这就是我想让它可部署的原因,所以它可以在 tomcat 上解压。这似乎很容易,但我无法让它工作。

显然上下文总是被加载两次。因为首先我得到了我使用(已经被阻止)的存储库的异常(@Repository)和 spring 的另一个异常“无法初始化上下文,因为已经存在根应用程序上下文”。

正如我已经读过的那样,我感到困惑,Spring 创建 2 个上下文是很正常的,尤其是使用 MVC。但是为什么会有例外呢?

我的 SpringApp 类看起来像这样

@EnableWebMvc
@ComponentScan
@EnableAutoConfiguration
public class LAuthServerApplication extends WebMvcConfigurerAdapter 

    @Autowired
    Environment env;

    public static void main(String[] args) 
    System.out.println("#################### Startup parameters ##############");
    for (String s : args) 
        System.out.println("Parameter: " + s);
    
    System.out.println("######################################################");
    SpringApplication.run(LAuthServerApplication.class, args);
    

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) 
    configurer.enable();
    

    @Bean
    public InternalResourceViewResolver viewResolver() 
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/pages/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
    

为了使其可部署,我将这个类添加到我的项目中:

@Configuration
public class AppServletInitializer extends SpringBootServletInitializer 
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) 
        return application.sources(Application.class);
    

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>ch.comlab.comweb</groupId>
    <artifactId>LAuth</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

    </properties>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
    </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-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
    </dependency>
    <dependency>
        <groupId>ch.comlab.comweb</groupId>
        <artifactId>Infrastructure</artifactId>
        <version>1.2.6</version>
    </dependency>

    </dependencies>
    <build>
    <plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
            <webResources>
            <resource>
                <directory>src/main/webapp</directory>
                <filtering>true</filtering>
            </resource> 
            </webResources>
        </configuration>
        </plugin>
        <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <resources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources/</directory>
            </resource>
            </resources>
        </configuration>
        </plugin>
    </plugins>
    </build>
    <repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
    </repositories>
    <pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
    </pluginRepositories>
    <name>LAuth</name>
</project>

【问题讨论】:

你见过Spring Boot Devtools吗?允许您在 IDE 中调试应用程序时动态调试和编辑 js 文件等文件(当未打包在 jar 中时)。无需部署在Tomcat中进行调试。 @Herr 你能显示你的 pom 文件吗?我已经做了一个带有战争配置的项目,要部署到 tomcat 中。我可以和你分享 Spring Boot War deployed to Tomcat的可能重复 @Mudassar 我确实试过了。它没有用,这就是我在这里问的原因 @Jesper 这也适用于可执行的战争吗? 【参考方案1】:

我相信这是由于来自 spring-boot-tomcat 的嵌入式服务器。

你能排除嵌入式tomcat并尝试吗? 例如:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

同时删除你在 spring-boot-starter-web 旁边提供的 tomcat 依赖项。

【讨论】:

【参考方案2】:

如果你使用 Spring Boot,你的配置会有点混乱和冗长。

您应该从插件列表中删除maven-war-plugin。因为spring-boot-maven-plugin 已经处理了这些事情(父母也是)。

接下来,您的配置删除一些代码并将LAuthServerApplication 和您的启动器合并为一个。并使用@SpringBootApplication 而不是单个注释(节省您的代码)。

@SpringBootApplication
public class LAuthServerApplication extends SpringBootServletInitializer 

    @Autowired
    Environment env;

    public static void main(String[] args) 
        System.out.println("#################### Startup parameters ##############");
        for (String s : args) 
            System.out.println("Parameter: " + s);
        
        System.out.println("######################################################");
        SpringApplication.run(LAuthServerApplication.class, args);
    

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


在你的application.properties 添加

spring.mvc.view.prefix=/WEB-INF/pages/
spring.mvc.view.suffix=.jsp

Spring Boot 已经为您配置了 Spring MVC,而您的配置会干扰它。除非您有非常特殊的需求,否则不要覆盖默认值。

现在重建并重新启动您的应用程序。

注意:恕我直言,将解压后的文件直接编辑到 tomcat 中是一个非常糟糕的主意,因为您确实想编辑源文件。您可能想看看JHipster 项目,而不是乱搞,它统一了 JS 和 Spring Boot 开发。

【讨论】:

谢谢,我尝试了您建议的更改。我仍然收到“无法初始化上下文,因为已经存在根应用程序上下文”。为什么直接在tomcat中编辑js文件是个坏主意?在我看来,它节省了很多时间。 因为您正在编辑已部署的文件,您需要将其复制回您的工作区以准备最终的工件。这将在未来的某个时候咬你。你的类路径中有web.xml 或另一个WebApplicationInitializer 吗?【参考方案3】:

我终于让它运行了,下面的 POM 和建议的代码更改来自 M.Denium 和 Dhanabalan:

<?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>ch.comlab.comweb</groupId>
<artifactId>LAuth</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>

<dependencies>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
    <exclusion>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
    </exclusions>
</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-el</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
    <groupId>ch.comlab.comweb</groupId>
    <artifactId>Infrastructure</artifactId>
    <version>1.2.6</version>
</dependency>

</dependencies>
<build>
<plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <resources>
        <resource>
            <filtering>true</filtering>
            <directory>src/main/resources/</directory>
        </resource>
        </resources>
    </configuration>
    </plugin>
</plugins>
</build>
<repositories>
<repository>
    <id>spring-releases</id>
    <url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
    <id>spring-releases</id>
    <url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
<name>LAuth</name>

【讨论】:

【参考方案4】:

如果你在线使用过spring initializer 默认pom文件是这样的 对于版本2.3.1.RELEASE

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

改成

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

清除缓存对我不起作用。唯一有效的就是这个。

【讨论】:

以上是关于部署在tomcat上的Spring Boot无法启动的主要内容,如果未能解决你的问题,请参考以下文章

部署在 Tomcat 上的 Spring Boot Rest API 提供 404 但独立工作一切正常

多个Spring Boot项目部署在一个Tomcat容器无法启动

Spring Boot在部署到Tomcat期间无法加载外部jar

在 tomcat 9 上部署 Spring Boot Web 应用程序时,出现错误“无法检索系统属性'spring.xml.ignore'”

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

部署到 Tomcat 10 后无法访问 Spring Boot 应用程序 [重复]