2013-01-18 1 views
2

Hibernate의 지연로드를 사용하고 있으며, web.xml에 OpenSessionInViewFilter를 추가 한 후 사용할 sessionFactory bean을 정의한 후에도 sessionFactory 누락 예외가 발생하기 시작했습니다. OpenSessionInViewFilter를 사용할 때 'sessionFactory'라는 Bean을 얻지 못했습니다.

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined 
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529) 
org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095) 
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277) 
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1097) 
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:242) 
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:227) 
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:171) 
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) 

This

같은 문제가 될 것 같다하지만 불행히도 나는 해결책을 이해하지 못했다. 나는 제안 된 해결책을 따른다. (sessionFactory를 root-spring.xml 파일로 옮긴다.) 나는 서버 시작을 제외하고 얻는다.

제가 누락 된 아이디어가 있습니까?

web.xml을

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

    <display-name>recommendationCrawler</display-name> 

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

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

    <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> 
    </filter> 

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

    <!--<listener>--> 
     <!--<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>--> 
    <!--</listener>--> 

    <!--<context-param>--> 
     <!--<param-name>log4jConfigLocation</param-name>--> 
     <!--<param-value>/WEB-INF/log4j.properties</param-value>--> 
    <!--</context-param>--> 

    <!-- listener to load the root application context --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
</web-app> 

봄-servlet.xml에

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:jee="http://www.springframework.org/schema/jee" 
     xmlns:lang="http://www.springframework.org/schema/lang" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:util="http://www.springframework.org/schema/util" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 
     http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> 


    <context:annotation-config/> 
    <context:component-scan base-package="net.crawler"/> 

    <mvc:annotation-driven/> 

    <bean id="jspViewResolver" 
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" 
        value="org.springframework.web.servlet.view.JstlView"/> 
     <property name="prefix" value="/WEB-INF/jsp/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 

    <bean id="messageSource" 
      class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
     <property name="basename" value="classpath:messages"/> 
     <property name="defaultEncoding" value="UTF-8"/> 
    </bean> 

    <bean id="propertyConfigurer" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
      p:location="/WEB-INF/jdbc.properties"/> 

    <bean id="dataSource" 
      class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" 
      p:driverClassName="${jdbc.driverClassName}" 
      p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" 
      p:password="${jdbc.password}"/> 


    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="configLocation"> 
      <value>classpath:hibernate.cfg.xml</value> 
     </property> 
     <property name="configurationClass"> 
      <value>org.hibernate.cfg.AnnotationConfiguration</value> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${jdbc.dialect}</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.hbm2ddl.auto">update</prop> 
      </props> 
     </property> 
     <property name="annotatedClasses"> 
      <list> 
       <value>net.crawler.dao.model.ScrapingProperties</value> 
      </list> 
     </property> 
    </bean> 

    <tx:annotation-driven/> 

    <bean id="transactionManager" 
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 

    <!--&lt;!&ndash; Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory &ndash;&gt;--> 
    <mvc:resources mapping="/resources/**" location="/resources/"/> 


</beans> 

프로젝트 구조 :

project structure

답변

4

은 applicationContext.xml

에 다음과 같은 구성을 이동하십시오
<bean id="propertyConfigurer" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
      p:location="/WEB-INF/jdbc.properties"/> 

    <bean id="dataSource" 
      class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" 
      p:driverClassName="${jdbc.driverClassName}" 
      p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" 
      p:password="${jdbc.password}"/> 


    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="configLocation"> 
      <value>classpath:hibernate.cfg.xml</value> 
     </property> 
     <property name="configurationClass"> 
      <value>org.hibernate.cfg.AnnotationConfiguration</value> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${jdbc.dialect}</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.hbm2ddl.auto">update</prop> 
      </props> 
     </property> 
     <property name="annotatedClasses"> 
      <list> 
       <value>net.crawler.dao.model.ScrapingProperties</value> 
      </list> 
     </property> 
    </bean> 

    <tx:annotation-driven/> 

    <bean id="transactionManager" 
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 
+0

나를 위해 일했습니다. 감사합니다. – special0ne

+0

우수합니다. 기꺼이 도와 드리겠습니다. –