简体   繁体   中英

Unable to start embedded container Spring Boot Application org.apache.catalina.LifecycleException: A child container failed during start

Try to run a mvc spring boot application with Gradle, i just want to start the Spring boot project with the following class: 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();
}
}

...when i try to run the programm on the Application , i get the following exception at the piece of code return super.getTomcatEmbeddedServletContainer(tomcat); :

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:535) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) at com.github.p4535992.Application.main(Application.java:50) at sun.reflect.NativeMethodAccessorImpl.invo ke0(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) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:116) at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.(TomcatEmbeddedServletContainer.java:83) at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:530) at com.github.p4535992.Application$1.getTomcatEmbeddedServletContainer(Application.java:101) at org.springframework.bo ot.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 common frames omitted Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardServer[-1]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:158) at org.apache.catalina.startup.Tomcat.start(Tomcat.java:356) at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:97) ... 19 common frames omitted Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Tomcat]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase. java:158) at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:791) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:152) ... 21 common frames omitted Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:158) at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:152) ... 23 common frames omitted Caused by: org.apache.catalina.LifecycleException: A child container failed during start at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:919) at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:152) ... 25 common frames omitted

Any suggestion?

Update the question:23-08-2016 after the M. Deinum comment

Not sure whether you resolved now. Mostly this like problem is due to incorrect version tomcat jar file in the dependencies.

I met similar problem today, what I did to resolve it:

  1. In the "IntelliJ IDEA" maven project windows, there is a button "Show Dependencis", click it.
  2. Search the "servlet", find out all the un-exected servlet jar, exclude them.
  3. The pom.xml will auto-add below lines

     <exclusions> <exclusion> <artifactId>servlet-api-2.5</artifactId> <groupId>org.mortbay.jetty</groupId> </exclusion> </exclusions> 

And also you could mvn dependency:tree to get the list and also find out the incorrect tomcat dependecy. Using IDE will be faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM