无法启动嵌入式容器Spring Boot Application org.apache.catalina.LifecycleException:子容器在启动期间失败

时间:2016-08-22 12:43:23

标签: java spring tomcat spring-boot

尝试使用Gradle运行mvc spring启动应用程序,我只想使用以下类启动Spring启动项目: 的的build.gradle

buildscript {
    ext {
        springBootVersion = '1.4.0.RELEASE'
        rdf4jVersion = '2.0'
       // tomcat.version = '7.0.59'
    }
    repositories {
        maven { url "http://repo.spring.io/libs-milestone" }
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}
//ext['tomcat.version'] = '9.0.0.M9'

apply plugin: 'java'
apply plugin: 'war'

apply plugin: 'eclipse'
apply plugin: 'spring-boot'

war {
    baseName = 'eat-basic'
    version =  '0.0.1'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    /* =====================*/
    /* SPRING BOOT          */
    /* =====================*/
    compile(group: 'org.springframework.boot', name: 'spring-boot-starter') {
        exclude(module: 'commons-logging')
        exclude(module: 'servlet-api')
        exclude(group: 'org.springframework.boot', module: 'spring-boot-starter-logging')
    }
    compile (group: 'org.springframework.boot', name: 'spring-boot-starter-web'){      
        exclude(module: 'servlet-api')      
    }
    /*Other Spring boot dependency*/
    compile (group: 'org.springframework.boot', name: 'spring-boot-devtools'){
        exclude(module: 'servlet-api')
    }

    /*Optional Spring boot */
    compile (group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc'){
        exclude(module: 'servlet-api')
    }
    compile (group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'){
        exclude(module: 'servlet-api')
    }
    compile (group: 'org.springframework.boot', name: 'spring-boot-starter-jooq'){
        exclude(module: 'servlet-api')
    }
    testCompile(group: 'org.springframework.boot', name:'spring-boot-starter-test'){
        exclude(module: 'commons-logging')
        exclude(module: 'servlet-api')
    }

    /* =============================*/
    /* Required dependency for JSP  */
    /* =============================*/
    compile (group: 'javax.servlet', name: 'jstl'){
        exclude(module: 'servlet-api')
    } /*include from spring boot plugin */
    compile group: 'javax.servlet', name: 'servlet-api' /*include from spring boot plugin */
    compile (group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper'){
        exclude(module: 'servlet-api')
    } /*include from spring boot plugin*/
    compile (group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core'){
        exclude(module: 'servlet-api')
    } /*include from spring boot plugin*/

    compile(group: 'javax.servlet.jsp', name: 'javax.servlet.jsp-api', version: '2.3.2-b02'){
        exclude(module: 'servlet-api')
    }
    compile group: 'javax.servlet', name: 'servlet-api', version: '2.5'

    /* ====================== */
    /* LOGGING */
    /* ====================== */
    compileOnly 'org.slf4j:slf4j-api:1.7.21'
    /* log4j-over-slf4j + jul-to-slf4j + jcl-over-slf4j + logback-classic*/
    compile(group: 'org.springframework.boot', name: 'spring-boot-starter-logging', version: '1.4.0.RELEASE'){
        exclude(group: 'ch.qos.logback', module:'logback-classic')
    }
    compile (group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.7') {
        exclude group: 'org.slf4j'
    }
}


eclipse {
    classpath {
         containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
         containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
    }
}

Application.java

@SpringBootApplication
public class Application extends SpringBootServletInitializer implements WebApplicationInitializer {
private static final org.slf4j.Logger logger =
        org.slf4j.LoggerFactory.getLogger(Application.class);

public static String FOLDER = "default";

public static void main(String[] args) {

    logger.info("EAT Playground\n");

    if (args.length > 0) {
        String folderAux = args[0];
        if (new File(folderAux).exists()){
            FOLDER  = folderAux;
        }else logger.info(folderAux + " folder not found");
    }
    logger.info("Reading from " + FOLDER);

    SpringApplication.run(Application.class, args);

    String port;
    if (System.getProperty("server.port") == null) {
        port = "8080";
        logger.info("Taking default port 8080. The value of the port can be changed, by adding the jvm option: -Dserver.port=8090");
    } else {
        port = System.getProperty("server.port");
        logger.info("server.port option found. Taking port " + port);
    }

    String serverUrl = "http://localhost:" + port; // path to your new file

    logger.info("Server started at " + serverUrl);

}

@Bean
public CacheManager cacheManager() {
    return new ConcurrentMapCacheManager("asset", "queries", "faqs", "page", "page-tree");
}

private Set<ErrorPage> pageHandlers;

@PostConstruct
private void init(){
    pageHandlers = new HashSet<>();
    pageHandlers.add(new ErrorPage(HttpStatus.NOT_FOUND,"/notfound.html"));
    pageHandlers.add(new ErrorPage(HttpStatus.FORBIDDEN,"/forbidden.html"));
}

/**
 * Method to adding a web application to Tomcat's web apps directory"
 * @return the {@link EmbeddedServletContainerFactory}
 */
@Bean
public EmbeddedServletContainerFactory servletContainerFactory() {
    return new TomcatEmbeddedServletContainerFactory() {
        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                Tomcat tomcat) {
            try {
                logger.info("Setting custom configuration for Mainstay:");
                //tomcat.setPort(Integer.valueOf(port));
                //tomcat.setContextPath(context);
                //tomcat.setErrorPages(pageHandlers);
                tomcat.addWebapp("/workbench", "/war/rdf4j-workbench.war");
                tomcat.addWebapp("/server", "/war/rdf4j-server.war");
            } catch (ServletException ex) {
                throw new IllegalStateException("Failed to add webapp", ex);
            }
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }

    };
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
}

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    SpringApplication.run(Application.class);
}
}

CacheConfiguratrion.java

Configuration
@EnableCaching
@Profile("!nocache")
public class CacheConfiguration {}

WebConfig.java

@Configuration
public class WebConfig extends WebMvcConfigurationSupport { 

@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
}

@Override
protected void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("forward:/index.html");
}

@Bean
public InternalResourceViewResolver defaultViewResolver() {
    return new InternalResourceViewResolver();
}
}

...当我尝试在应用程序上运行程序时,我在代码return super.getTomcatEmbeddedServletContainer(tomcat);处得到以下异常:

  

org.springframework.context.ApplicationContextException:无法启动嵌入式容器;嵌套异常是org.springframework.boot.context.embedded.EmbeddedServletContainerException:无法启动嵌入式Tomcat       at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137)       在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:535)       at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)       在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)       在org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369)       在org.springframework.boot.SpringApplication.run(SpringApplication.java:313)       在org.springframework.boot.SpringApplication.run(SpringApplication.java:1185)       在org.springframework.boot.SpringApplication.run(SpringApplication.java:1174)       在com.github.p4535992.Application.main(Application.java:50)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       at java.lang.reflect.Method.invoke(Method.java:498)       在org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)   引起:org.springframework.boot.context.embedded.EmbeddedServletContainerException:无法启动嵌入式Tomcat       at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:116)       在org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer。(TomcatEmbeddedServletContainer.java:83)       at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:530)       在com.github.p4535992.Application $ 1.getTomcatEmbeddedServletContainer(Application.java:101)       at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:176)       at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:164)       at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134)       ...省略了13个常用帧   引起:org.apache.catalina.LifecycleException:无法启动组件[StandardServer [-1]]       在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:158)       在org.apache.catalina.startup.Tomcat.start(Tomcat.java:356)       at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:97)       ...省略了19个常见帧   引起:org.apache.catalina.LifecycleException:无法启动组件[StandardService [Tomcat]]       在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:158)       at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:791)       在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:152)       ...省略了21个常用帧   引起:org.apache.catalina.LifecycleException:无法启动组件[StandardEngine [Tomcat]]       在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:158)       at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)       在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:152)       ...省略了23个常见帧   引起:org.apache.catalina.LifecycleException:子容器在启动期间失败       在org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:919)       在org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)       在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:152)       ...省略了25个常见帧

有什么建议吗?

更新问题:在M. Deinum发表评论之后的23-08-2016

1 个答案:

答案 0 :(得分:0)

不确定你现在是否已经解决了。 大多数情况下这个问题是由于依赖项中的版本tomcat jar文件不正确。

我今天遇到了类似的问题,我采取了解决问题的方法:

  1. 在“IntelliJ IDEA”maven项目窗口中,有一个“Show Dependencis”按钮,单击它。
  2. 搜索“servlet”,找出所有未执行的servlet jar,将其排除。
  3. pom.xml会自动添加以下行

    <exclusions>
        <exclusion>
            <artifactId>servlet-api-2.5</artifactId>
            <groupId>org.mortbay.jetty</groupId>
        </exclusion>
    </exclusions>
    
  4. 你也可以mvn dependency:tree获取列表,并找出不正确的tomcat依赖性。使用IDE会更快。