部署在 tomcat 上的 Spring Boot 无法启动 [英] Spring boot deployed on tomcat won't start

查看:44
本文介绍了部署在 tomcat 上的 Spring Boot 无法启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这就是我想让它可部署的原因,这样它就可以在 tomcat 上解压.这似乎很容易,但我无法让它发挥作用.

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

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

我的 SpringApp 类看起来像这样

@EnableWebMvc@ComponentScan@EnableAutoConfiguration公共类 LAuthServerApplication 扩展了 WebMvcConfigurerAdapter {@自动连线环境环境;公共静态无效主(字符串 [] args){System.out.println("###################启动参数###############启动参数###############for (String s : args) {System.out.println("参数:" + s);}System.out.println("###################################################");SpringApplication.run(LAuthServerApplication.class, args);}@覆盖public void configureDefaultServletHandling(DefaultServletHandlerConfigurer 配置器) {配置器.启用();}@豆角,扁豆public InternalResourceViewResolver viewResolver() {InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();viewResolver.setPrefix("/WEB-INF/pages/");viewResolver.setSuffix(".jsp");返回视图解析器;}

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

@Configuration公共类 AppServletInitializer 扩展 SpringBootServletInitializer {@覆盖受保护的 SpringApplicationBuilder 配置(SpringApplicationBuilder 应用程序){返回 application.sources(Application.class);}}

POM:

<modelVersion>4.0.0</modelVersion><groupId>ch.comlab.comweb</groupId><artifactId>LAuth</artifactId><version>1.0.0</version><包装>战争</包装><属性><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></属性><父母><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.5.RELEASE</version></父母><依赖项><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><范围>提供</范围></依赖><依赖><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId><范围>提供</范围></依赖><依赖><groupId>javax.servlet</groupId><artifactId>jstl</artifactId></依赖><依赖><groupId>org.springframework.security</groupId><artifactId>spring-security-web</artifactId></依赖><依赖><groupId>org.springframework.security</groupId><artifactId>spring-security-config</artifactId></依赖><依赖><groupId>ch.comlab.comweb</groupId><artifactId>基础设施</artifactId><version>1.2.6</version></依赖></依赖项><构建><插件><插件><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>2.6</version><配置><网络资源><资源><目录>src/main/webapp</directory><filtering>true</filtering></资源></webResources></配置></插件><插件><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><配置><资源><资源><filtering>true</filtering><目录>src/main/resources/</directory></资源></资源></配置></插件></plugins></build><存储库><存储库><id>spring-releases</id><url>https://repo.spring.io/libs-release</url></repository></repositories><插件库><插件库><id>spring-releases</id><url>https://repo.spring.io/libs-release</url></pluginRepository></pluginRepositories><name>LAuth</name></项目>

解决方案

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

<modelVersion>4.0.0</modelVersion><groupId>ch.comlab.comweb</groupId><artifactId>LAuth</artifactId><version>1.0.0</version><包装>战争</包装><属性><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></属性><父母><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.5.RELEASE</version></父母><依赖项><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><排除事项><排除><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></排除></排除项></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><范围>提供</范围></依赖><依赖><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-el</artifactId><范围>提供</范围></依赖><依赖><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></依赖><依赖><groupId>org.springframework.security</groupId><artifactId>spring-security-web</artifactId></依赖><依赖><groupId>org.springframework.security</groupId><artifactId>spring-security-config</artifactId></依赖><依赖><groupId>ch.comlab.comweb</groupId><artifactId>基础设施</artifactId><version>1.2.6</version></依赖></依赖项><构建><插件><插件><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><配置><资源><资源><filtering>true</filtering><目录>src/main/resources/</directory></资源></资源></配置></插件></plugins></build><存储库><存储库><id>spring-releases</id><url>https://repo.spring.io/libs-release</url></repository></repositories><插件库><插件库><id>spring-releases</id><url>https://repo.spring.io/libs-release</url></pluginRepository></pluginRepositories><name>LAuth</name>

I have a working Spring application that contains a embedded Tomcat. As a executable war, it works fine. As I'm going to use AngularJ for the front end, it isn't very practical to have the Spring project in an executable jar, as I will do debugging in the browser and want to be able to quickly edit the js source files. If they are saved in an archive, this is annoying.

That's why I want to make it deployable, so it would get unpacked on a tomcat. It seems to be that easy, but I can't get it to work.

The context always gets loaded twice apparently. Because of that first I get an exception of the repository (@Repository) I use (already blocked) and another exception from spring "Cannot initialize context because there is already a root application context present".

I confused as I already read, that is quite normal that Spring creates 2 contexts, especially with MVC. But why the exceptions then?

My SpringApp class looks like this

@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;
    }

To make it deployable I added this class to my project:

@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>

解决方案

I finally got it to run, with the following POM and the suggested Code change from M.Denium and 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>

这篇关于部署在 tomcat 上的 Spring Boot 无法启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆