WebAppInitializer无法强制转换为javax.servlet.ServletContainerInitializer

时间:2014-05-13 12:46:29

标签: spring maven tomcat7

我在运行mvn tomcat7:run时遇到以下堆栈跟踪:

java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[/blog-demo]]
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:188)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[/blog-demo]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    ... 6 more
Caused by: java.lang.ClassCastException: com.replaceme.WebAppInitializer cannot be cast to javax.servlet.ServletContainerInitializer
    at org.apache.catalina.startup.ContextConfig.getServletContainerInitializer(ContextConfig.java:1670)
    at org.apache.catalina.startup.ContextConfig.getServletContainerInitializers(ContextConfig.java:1652)
    at org.apache.catalina.startup.ContextConfig.processServletContainerInitializers(ContextConfig.java:1562)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1270)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:376)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5322)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 6 more

这是我的WebAppInitializer类:

public class WebAppInitializer extends AbstractSecurityWebApplicationInitializer {
    @Override
    public void beforeSpringSecurityFilterChain(ServletContext servletContext) {
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();

        ConfigurableEnvironment env = rootContext.getEnvironment();
        env.setDefaultProfiles("production", env.getProperty("user.name"));

        rootContext.scan("com.replaceme.config");

        servletContext.addListener(new ContextLoaderListener(rootContext));

        ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");
    }
}

我在src/main/resources/META-INF/services中添加了一个名为javax.servlet.ServletContainerInitializer的文本文件,其中包含类标识com.replaceme.WebAppInitializer

这是我的maven依赖项:

[INFO] +- org.hibernate:hibernate-core:jar:4.3.5.Final:compile
[INFO] |  +- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile
[INFO] |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[INFO] |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.0.Final:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.4.Final:compile
[INFO] |  |  +- (org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile - omitted for duplicate)
[INFO] |  |  \- (org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile - omitted for duplicate)
[INFO] |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] |  +- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  \- org.jboss:jandex:jar:1.1.0.Final:compile
[INFO] +- org.hibernate:hibernate-validator:jar:5.1.0.Final:compile
[INFO] |  +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  +- (org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile - omitted for duplicate)
[INFO] |  \- com.fasterxml:classmate:jar:1.0.0:compile
[INFO] +- ch.qos.logback:logback-classic:jar:1.0.13:runtime
[INFO] |  +- ch.qos.logback:logback-core:jar:1.0.13:runtime
[INFO] |  \- (org.slf4j:slf4j-api:jar:1.7.5:runtime - omitted for duplicate)
[INFO] +- org.slf4j:slf4j-api:jar:1.7.5:compile
[INFO] +- org.slf4j:jcl-over-slf4j:jar:1.7.5:compile
[INFO] |  \- (org.slf4j:slf4j-api:jar:1.7.5:compile - omitted for duplicate)
[INFO] +- org.codehaus.janino:janino:jar:2.6.1:compile
[INFO] |  \- org.codehaus.janino:commons-compiler:jar:2.6.1:compile
[INFO] +- org.springframework:spring-context:jar:4.0.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:4.0.3.RELEASE:compile
[INFO] |  |  +- (aopalliance:aopalliance:jar:1.0:compile - omitted for duplicate)
[INFO] |  |  +- (org.springframework:spring-beans:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] |  |  \- (org.springframework:spring-core:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] |  +- org.springframework:spring-beans:jar:4.0.3.RELEASE:compile
[INFO] |  |  \- (org.springframework:spring-core:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] |  +- org.springframework:spring-core:jar:4.0.3.RELEASE:compile
[INFO] |  \- org.springframework:spring-expression:jar:4.0.3.RELEASE:compile
[INFO] |     \- (org.springframework:spring-core:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] +- org.springframework:spring-jdbc:jar:4.0.3.RELEASE:compile
[INFO] |  +- (org.springframework:spring-beans:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-core:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] |  \- org.springframework:spring-tx:jar:4.0.3.RELEASE:compile
[INFO] |     +- (org.springframework:spring-beans:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] |     \- (org.springframework:spring-core:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] +- org.springframework:spring-orm:jar:4.0.3.RELEASE:compile
[INFO] |  +- (org.springframework:spring-beans:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-core:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-jdbc:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] |  \- (org.springframework:spring-tx:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] +- org.springframework:spring-webmvc:jar:4.0.3.RELEASE:compile
[INFO] |  +- (org.springframework:spring-beans:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-context:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-core:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-expression:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] |  \- org.springframework:spring-web:jar:4.0.3.RELEASE:compile
[INFO] |     +- (org.springframework:spring-aop:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] |     +- (org.springframework:spring-beans:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] |     +- (org.springframework:spring-context:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] |     \- (org.springframework:spring-core:jar:4.0.3.RELEASE:compile - omitted for duplicate)
[INFO] +- org.springframework.security:spring-security-config:jar:3.2.3.RELEASE:compile
[INFO] |  +- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- org.springframework.security:spring-security-core:jar:3.2.3.RELEASE:compile
[INFO] |  |  +- (aopalliance:aopalliance:jar:1.0:compile - omitted for duplicate)
[INFO] |  |  +- (org.springframework:spring-aop:jar:3.2.8.RELEASE:compile - omitted for conflict with 4.0.3.RELEASE)
[INFO] |  |  +- (org.springframework:spring-beans:jar:3.2.8.RELEASE:compile - omitted for conflict with 4.0.3.RELEASE)
[INFO] |  |  +- (org.springframework:spring-context:jar:4.0.3.RELEASE:compile - version managed from 3.2.8.RELEASE; omitted for duplicate)
[INFO] |  |  +- (org.springframework:spring-core:jar:3.2.8.RELEASE:compile - omitted for conflict with 4.0.3.RELEASE)
[INFO] |  |  \- (org.springframework:spring-expression:jar:3.2.8.RELEASE:compile - omitted for conflict with 4.0.3.RELEASE)
[INFO] |  +- (org.springframework:spring-aop:jar:3.2.8.RELEASE:compile - omitted for conflict with 4.0.3.RELEASE)
[INFO] |  +- (org.springframework:spring-beans:jar:3.2.8.RELEASE:compile - omitted for conflict with 4.0.3.RELEASE)
[INFO] |  +- (org.springframework:spring-context:jar:4.0.3.RELEASE:compile - version managed from 3.2.8.RELEASE; omitted for duplicate)
[INFO] |  \- (org.springframework:spring-core:jar:3.2.8.RELEASE:compile - omitted for conflict with 4.0.3.RELEASE)
[INFO] +- org.springframework.security:spring-security-web:jar:3.2.3.RELEASE:compile
[INFO] |  +- (aopalliance:aopalliance:jar:1.0:compile - omitted for duplicate)
[INFO] |  +- (org.springframework.security:spring-security-core:jar:3.2.3.RELEASE:compile - omitted for duplicate)
[INFO] |  +- (org.springframework:spring-beans:jar:3.2.8.RELEASE:compile - omitted for conflict with 4.0.3.RELEASE)
[INFO] |  +- (org.springframework:spring-context:jar:4.0.3.RELEASE:compile - version managed from 3.2.8.RELEASE; omitted for duplicate)
[INFO] |  +- (org.springframework:spring-core:jar:3.2.8.RELEASE:compile - omitted for conflict with 4.0.3.RELEASE)
[INFO] |  +- (org.springframework:spring-expression:jar:3.2.8.RELEASE:compile - omitted for conflict with 4.0.3.RELEASE)
[INFO] |  \- (org.springframework:spring-web:jar:3.2.8.RELEASE:compile - omitted for conflict with 4.0.3.RELEASE)
[INFO] +- org.springframework.data:spring-data-jpa:jar:1.5.2.RELEASE:compile
[INFO] |  +- org.springframework.data:spring-data-commons:jar:1.7.2.RELEASE:compile
[INFO] |  |  +- (org.springframework:spring-core:jar:3.2.8.RELEASE:compile - omitted for conflict with 4.0.3.RELEASE)
[INFO] |  |  +- (org.springframework:spring-beans:jar:3.2.8.RELEASE:compile - omitted for conflict with 4.0.3.RELEASE)
[INFO] |  |  +- (org.slf4j:slf4j-api:jar:1.7.5:compile - version managed from 1.7.6; omitted for duplicate)
[INFO] |  |  \- (org.slf4j:jcl-over-slf4j:jar:1.7.5:runtime - version managed from 1.7.6; omitted for duplicate)
[INFO] |  +- (org.springframework:spring-orm:jar:4.0.3.RELEASE:compile - version managed from 3.2.8.RELEASE; omitted for duplicate)
[INFO] |  +- (org.springframework:spring-context:jar:4.0.3.RELEASE:compile - version managed from 3.2.8.RELEASE; omitted for duplicate)
[INFO] |  +- (org.springframework:spring-aop:jar:3.2.8.RELEASE:compile - omitted for conflict with 4.0.3.RELEASE)
[INFO] |  +- (org.springframework:spring-tx:jar:3.2.8.RELEASE:compile - omitted for conflict with 4.0.3.RELEASE)
[INFO] |  +- (org.springframework:spring-beans:jar:3.2.8.RELEASE:compile - omitted for conflict with 4.0.3.RELEASE)
[INFO] |  +- (org.springframework:spring-core:jar:3.2.8.RELEASE:compile - omitted for conflict with 4.0.3.RELEASE)
[INFO] |  +- org.aspectj:aspectjrt:jar:1.7.4:compile
[INFO] |  +- (org.slf4j:slf4j-api:jar:1.7.5:compile - version managed from 1.7.6; omitted for duplicate)
[INFO] |  \- (org.slf4j:jcl-over-slf4j:jar:1.7.5:runtime - version managed from 1.7.6; omitted for duplicate)
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] +- org.thymeleaf:thymeleaf:jar:2.1.2.RELEASE:compile
[INFO] |  +- ognl:ognl:jar:3.0.6:compile
[INFO] |  +- (org.javassist:javassist:jar:3.16.1-GA:compile - omitted for conflict with 3.18.1-GA)
[INFO] |  \- (org.slf4j:slf4j-api:jar:1.7.5:compile - version managed from 1.6.1; omitted for duplicate)
[INFO] +- org.thymeleaf:thymeleaf-spring4:jar:2.1.2.RELEASE:compile
[INFO] |  +- (org.thymeleaf:thymeleaf:jar:2.1.2.RELEASE:compile - omitted for duplicate)
[INFO] |  \- (org.slf4j:slf4j-api:jar:1.7.5:compile - version managed from 1.6.1; omitted for duplicate)
[INFO] +- cglib:cglib:jar:3.1:compile
[INFO] |  \- org.ow2.asm:asm:jar:4.2:compile

编辑:它也不会使用以下课程:

public class WebAppInitializer implements WebApplicationInitializer {
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();

        ConfigurableEnvironment env = rootContext.getEnvironment();
        env.setDefaultProfiles("production", env.getProperty("user.name"));

        rootContext.scan("com.replaceme.config");

        servletContext.addListener(new ContextLoaderListener(rootContext));

        ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");
    }
}

2 个答案:

答案 0 :(得分:1)

扩展类com.replaceme.WebAppInitializer的类AbstractSecurityWebApplicationInitializer不是类javax.servlet.ServletContainerInitializer的子类型。因此,不允许您将一种类型的对象类型转换为另一种类型的对象。简单地说你试图将狗变成驴子,这就是为什么ClassCastException被抛出的原因。不能在不在同一继承树中的对象类型之间进行转换

答案 1 :(得分:1)

WebApplicationInitializer(来自Spring)不是ServletContainerInitializer(来自Servlet规范)。所以只需从services文件夹中删除文件javax.servlet.ServletContainerInitializer。 Spring会自动检测您的WebAppInitializer(请参阅here