定义OpenSessionInViewFilter时出错

时间:2014-12-10 13:56:43

标签: java hibernate spring-mvc

我来自LazyInitializationException with hibernate and spring mvc,但现在问题是这个错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:660)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1157)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:280)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:962)
at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:189)
at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:174)
at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:125)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

我的web.xml是:

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

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

     <!-- Hibernate OpenSessionInView filter -->
    <filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>sessionFactoryBeanName</param-name>
            <param-value>sessionFactory</param-value>
        </init-param>
    </filter>

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

</web-app>

在我的servlet-context.xml中,Session Factory的定义如下:

...

<!-- Hibernate session factory -->
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource"/>
    <!-- Annotated hibernate clasess -->
    <beans:property name="packagesToScan" value="org.example.myproject.domains"/>

    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.connection.pool_size">${hibernate.pool_size}</beans:prop>
            <beans:prop key="hibernate.dialect">${hibernate.dialect}</beans:prop>
            <beans:prop key="hibernate.cache.provider_class">${hibernate.provider_class}</beans:prop>
            <beans:prop key="hibernate.show_sql">${hibernate.show_sql}</beans:prop>
            <beans:prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl}</beans:prop>
        </beans:props>
    </beans:property>

</beans:bean>

...

为什么这是hapening?好像我有一个错误的*.xml定义,你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

您不必删除root-context.xml,因为您必须将所有bean放在一起分享,并将可见设为web.xmlservlet-context.xml以及您拥有的hibernateFilter,因为现在他“没有看到任何sessionFactory bean。请查看相关帖子:Spring, Hibernate Lazy Loading, sessionFactory, and OpenSessionInViewFilter

祝你好运!