有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法启动嵌入式容器Spring Boot应用程序org。阿帕奇。卡塔琳娜。LifecycleException:启动期间子容器失败

尝试用Gradle运行mvc spring boot应用程序,我只想用以下类启动spring boot项目: 构建。格拉德尔

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'
    }
}

应用程序。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);
}
}

trion。java

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

网络配置。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: 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.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) 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.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 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

有什么建议吗

更新问题:2016年8月23日,在M.Deinum评论之后


共 (1) 个答案

  1. # 1 楼答案

    不确定你现在是否决定了。 这类问题主要是由于依赖项中的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>
      

    您还可以mvn dependency:tree获取列表,并找出不正确的tomcat依赖项。使用IDE会更快