복붙노트

[SPRING] Spring, Hibernate Lazy Loading, sessionFactory 및 OpenSessionInViewFilter

SPRING

Spring, Hibernate Lazy Loading, sessionFactory 및 OpenSessionInViewFilter

나는 Hibernate의 게으른 로딩을 사용하고 있으며, openSessionInViewFilter를 사용하기 위해 web.xml에 필터를 정의한 후에도 sessionFactory에 예외가 발생하고있다.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.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>
<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>
       org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping> 
</web-app>

내 servlet-context.xml에는 다음과 같은 세션 및 트랜잭션 관리자 정의가 있습니다.

<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource"/>
    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.dialect">${connection.dialect}</beans:prop>
            <beans:prop key="hibernate.show_sql">true</beans:prop>
       </beans:props>
    </beans:property>
    <beans:property name="annotatedClasses">
        <beans:list>
            <beans:value>example.EntityA</beans:value>
            <beans:value>example.EntityB</beans:value>
        </beans:list>
    </beans:property>
</beans:bean>

<beans:bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <beans:property name="sessionFactory" ref="sessionFactory"/>
</beans:bean>
<tx:annotation-driven transaction-manager="transactionManager" />

나는 여전히 다음 예외를 얻는다 :

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined

sessionFactoryBeanName 특성을 정의하려고 시도했지만 결과가 변경되지 않습니다. 내가 뭘 잘못하고 있죠?

해결법

  1. ==============================

    1.OpenSessionInViewFilter에서 사용할 수 있도록 루트 웹 응용 프로그램 컨텍스트 (예 : /WEB-INF/spring/root-context.xml)에서 sessionFactory를 선언해야합니다.

    OpenSessionInViewFilter에서 사용할 수 있도록 루트 웹 응용 프로그램 컨텍스트 (예 : /WEB-INF/spring/root-context.xml)에서 sessionFactory를 선언해야합니다.

  2. ==============================

    2.servlet-context.xml에 sesstionFactory Bean을 넣어야합니다.

    servlet-context.xml에 sesstionFactory Bean을 넣어야합니다.

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="annotatedClasses">
        .....
    </property>
    <property name="hibernateProperties">
        ......
    </property>
    <property name="schemaUpdate" value="true" />
    

  3. ==============================

    3.이것은 sessionFactory 빈을 검색하는 org.springframework.orm.hibernate3.support.OpenSessionInViewFilter 클래스 내의 코드 부분입니다.

    이것은 sessionFactory 빈을 검색하는 org.springframework.orm.hibernate3.support.OpenSessionInViewFilter 클래스 내의 코드 부분입니다.

    /**
     * Look up the SessionFactory that this filter should use.
     * <p>The default implementation looks for a bean with the specified name
     * in Spring's root application context.
     * @return the SessionFactory to use
     * @see #getSessionFactoryBeanName
     */
    protected SessionFactory lookupSessionFactory() {
        if (logger.isDebugEnabled()) {
            logger.debug("Using SessionFactory '" + getSessionFactoryBeanName() + "' for OpenSessionInViewFilter");
        }
        WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
        return wac.getBean(getSessionFactoryBeanName(), SessionFactory.class);
    }
    

    WebApplicationContextUtils 클래스를 사용하여 세션 팩토리 빈을로드한다는 것을 알 수 있습니다. 이 클래스에 대한 API (http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/context/support/WebApplicationContextUtils.html)는 다음과 같이 설명합니다.

    따라서 Spring에서 제공 한 OpenSessionInViewFilter 기능을 사용하려면 root 컨텍스트에서 sessionFactory를 선언해야합니다.

  4. from https://stackoverflow.com/questions/8574552/spring-hibernate-lazy-loading-sessionfactory-and-opensessioninviewfilter by cc-by-sa and MIT license