OpenSessionInViewFilter无法强制转换为javax.servlet.Filter

时间:2013-03-30 06:52:12

标签: java spring hibernate servlet-filters open-session-in-view

当我尝试在Tomcat7中启动Java EE Spring + Hibernate应用程序时,我遇到了异常。这曾经有用,但我想我在迁移到Tomcat7时搞砸了一些东西。

是个例外
Mar 29, 2013 11:38:33 PM org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter hibernateFilter
java.lang.ClassCastException: org.springframework.orm.hibernate3.support.OpenSessionInViewFilter cannot be cast to javax.servlet.Filter
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:248)
    at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:368)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:98)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4193)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4801)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:988)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:771)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:988)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:275)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:427)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:649)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:585)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:415)
Mar 29, 2013 11:38:33 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error filterStart
Mar 29, 2013 11:38:33 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/ImageWar] startup failed due to previous errors

我的web.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Attach a Hibernate session to each request using this filter -->
    <filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>
            org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
        </filter-class>
        <init-param>
            <param-name>sessionFactoryBeanName</param-name>
            <param-value>sessionFactory</param-value>
        </init-param>
        <init-param>
            <param-name>flushMode</param-name>
            <param-value>AUTO</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>/plus/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>           

    <servlet>
        <servlet-name>ImageWar</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>MyApp</servlet-name>
        <url-pattern>/xyz/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/ImageWar-dataSource.xml</param-value>
    </context-param>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

我所有的Spring jar都是3.1.3.RELEASE,我有以下两个Hibernate依赖项:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.8.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.3.0.Final</version>
    </dependency>

我正在“提供”一些标准的JEE罐子:

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>

以下是mvn dependency:tree的输出:

[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ ImageWar ---
[INFO] com.sodapopsoftware:ImageWar:war:0.0.1-SNAPSHOT
[INFO] +- javax.servlet:jstl:jar:1.2:provided
[INFO] +- javax.servlet:javax.servlet-api:jar:3.0.1:provided
[INFO] +- javax.servlet.jsp:javax.servlet.jsp-api:jar:2.2.1:provided
[INFO] +- javax.el:javax.el-api:jar:2.2.2:provided
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.6.4:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.6.4:compile
[INFO] |  \- log4j:log4j:jar:1.2.16:compile
[INFO] +- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] +- commons-beanutils:commons-beanutils:jar:1.8.3:compile
[INFO] +- com.google.guava:guava:jar:13.0.1:compile
[INFO] +- org.imgscalr:imgscalr-lib:jar:4.2:compile
[INFO] +- taglibs:standard:jar:1.1.2:compile
[INFO] +- commons-fileupload:commons-fileupload:jar:1.2.2:compile
[INFO] +- org.apache.commons:commons-io:jar:1.3.2:compile
[INFO] +- junit:junit:jar:4.8.2:test
[INFO] +- org.easymock:easymock:jar:3.1:test
[INFO] |  +- cglib:cglib-nodep:jar:2.2.2:test
[INFO] |  \- org.objenesis:objenesis:jar:1.2:test
[INFO] +- org.springframework:spring-test:jar:3.1.3.RELEASE:compile
[INFO] +- org.springframework:spring-core:jar:3.1.3.RELEASE:compile
[INFO] |  \- org.springframework:spring-asm:jar:3.1.3.RELEASE:compile
[INFO] +- org.springframework:spring-aop:jar:3.1.3.RELEASE:compile
[INFO] |  +- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  \- org.springframework:spring-beans:jar:3.1.3.RELEASE:compile
[INFO] +- org.springframework:spring-web:jar:3.1.3.RELEASE:compile
[INFO] |  \- org.springframework:spring-context:jar:3.1.3.RELEASE:compile
[INFO] +- org.springframework:spring-webmvc:jar:3.1.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-context-support:jar:3.1.3.RELEASE:compile
[INFO] |  \- org.springframework:spring-expression:jar:3.1.3.RELEASE:compile
[INFO] +- org.springframework:spring-jdbc:jar:3.1.3.RELEASE:compile
[INFO] |  \- org.springframework:spring-tx:jar:3.1.3.RELEASE:compile
[INFO] +- org.springframework:spring-orm:jar:3.1.3.RELEASE:compile
[INFO] +- mysql:mysql-connector-java:jar:5.1.9:compile
[INFO] +- c3p0:c3p0:jar:0.9.1.1:compile
[INFO] +- org.hibernate:hibernate-core:jar:3.6.8.Final:compile
[INFO] |  +- antlr:antlr:jar:2.7.6:compile
[INFO] |  +- commons-collections:commons-collections:jar:3.1:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  +- org.hibernate:hibernate-commons-annotations:jar:3.2.0.Final:compile
[INFO] |  +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile
[INFO] |  \- javax.transaction:jta:jar:1.1:compile
[INFO] +- org.hibernate:hibernate-validator:jar:4.3.0.Final:compile
[INFO] |  +- javax.validation:validation-api:jar:1.0.0.GA:compile
[INFO] |  \- org.jboss.logging:jboss-logging:jar:3.1.0.CR2:compile
[INFO] \- javassist:javassist:jar:3.12.1.GA:compile

知道我为什么会遇到这个例外吗?

1 个答案:

答案 0 :(得分:4)

听起来你在WEB-INF/lib中有一个servlet API的版本。如果你正在使用maven,找到有问题的依赖项,并确保将它的依赖项排除在servlet-api之外。 mvn dependency:tree的输出有助于追踪不需要的传递依赖性。

编辑:

如果一切看起来都是正确的,并且你仍然遇到这个问题,我用一种技术来找出一个类的加载位置:

Filter.class.getProtectionDomain().getCodeSource().getLocation()

它将为您提供加载类的jar /文件夹的位置。我还建议您尝试在独立的应用程序服务器中部署Web应用程序,以排除IDE(例如服务器/部署插件)作为问题的根源。