Java 如何配置OpenEntityManagerViewFilter Spring MVC

Java 如何配置OpenEntityManagerViewFilter Spring MVC,java,hibernate,spring-mvc,lazy-initialization,hibernate-entitymanager,Java,Hibernate,Spring Mvc,Lazy Initialization,Hibernate Entitymanager,我想使用OpenEntityManagerInViewFilter来避免懒散的初始化加载。这是我的web.xml配置: <context-param> <param-name>contextConfigLocation</param-name> <param-value> WEB-INF/mvc-dispatcher-servlet.xml </param-value> </context

我想使用
OpenEntityManagerInViewFilter
来避免懒散的初始化加载。这是我的web.xml配置:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        WEB-INF/mvc-dispatcher-servlet.xml
    </param-value>

</context-param>

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

<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

有人知道我做错了什么吗?

原来是因为我将contextConfigLocation配置为:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
    WEB-INF/mvc-dispatcher-servlet.xml
</param-value>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
    <filter-name>oemInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    <init-param>
        <param-name>entityManagerFactoryBeanName</param-name>
        <param-value>entityManagerFactory</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>oemInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

上下文配置位置
WEB-INF/mvc-dispatcher-servlet.xml

dispatcher servlet与applicationContext不同,它被加载了两次或更多,因此解决方案是
1.创建applicationContext.xml, 2.然后将entitymanager bean从mvc dispatcher移动到applicationContext.xml。 3.My web.xml现在看起来像这样:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
    WEB-INF/mvc-dispatcher-servlet.xml
</param-value>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
    <filter-name>oemInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    <init-param>
        <param-name>entityManagerFactoryBeanName</param-name>
        <param-value>entityManagerFactory</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>oemInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

org.springframework.web.context.ContextLoaderListener
oemInViewFilter
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
EntityManager工厂名称
实体管理工厂
oemInViewFilter
/*
要求