当使用OpenSessionInViewFilter时,获取没有名为“sessionFactory”的bean错误 [英] Getting No bean named 'sessionFactory' error when using OpenSessionInViewFilter
问题描述
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为sessionFactory的bean
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。摘要ApplicationPlanner lookupSessionFactory(OpenSessionInViewFilter.java:227)
org.springframework。 orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:171)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
这个似乎是同样的问题,但不幸的是我不明白解决方案。
我按照建议的解决方案(将sessionFactory移动到一个名为root-spring.xml的文件),我得到了服务器启动的例外。
任何想法我失踪了?
web.xml
<?xml version =1.0encoding =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_IDversion =2.5>
< display-name> recommendationsCrawler< / 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>
< listener-class> org.springframework.web.context.ContextLoaderListener< / listener-class>
< / listener>
< / web-app>
Spring-servlet.xml
<?xml version =1.0encoding =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:// $
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:注释驱动/>
< bean id =jspViewResolver
class =org.springframework.web.servlet.view.InternalResourceViewResolver>
< property name =viewClass
value =org.springframework.web.servlet.view.JstlView/>
< property name =prefixvalue =/ WEB-INF / jsp //>
< property name =suffixvalue =。jsp/>
< / bean>
< bean id =messageSource
class =org.springframework.context.support.ReloadableResourceBundleMessageSource>
< property name =basenamevalue =classpath:messages/>
< property name =defaultEncodingvalue =UTF-8/>
< / bean>
< bean id =propertyConfigurer
class =org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
p:location =/ WEB-INF / jdbc。属性/>
< bean id =dataSource
class =org.apache.commons.dbcp.BasicDataSourcedestroy-method =close
p:driverClassName =$ { jdbc.driverClassName}
p:url =$ {jdbc.databaseurl}p:username =$ {jdbc.username}
p:password =$ {jdbc.password}/>
< bean id =sessionFactoryclass =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =configLocation>
< value> classpath:hibernate.cfg.xml< / value>
< / property>
< property name =configurationClass>
< value> org.hibernate.cfg.AnnotationConfiguration< / value>
< / property>
< property name =hibernateProperties>
<道具>
< 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:注释驱动/>
< bean id =transactionManager
class =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
<! - & lt;!& ndash;通过在$ {webappRoot} / resources / directory& ndash;> - >中有效地提供静态资源来处理/ resource / **的HTTP GET请求。
< mvc:resources mapping =/ resources / **location =/ resources //>
< / beans>
项目结构:
尝试将以下配置移动到applicationContext.xml
< bean id =propertyConfigurer
class =org。 spring,...
< bean id =dataSource
class =org.apache.commons.dbcp.BasicDataSourcedestroy-method =close
p:driverClassName =$ { jdbc.driverClassName}
p:url =$ {jdbc.databaseurl}p:username =$ {jdbc.username}
p:password =$ {jdbc.password}/>
< bean id =sessionFactoryclass =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =configLocation>
< value> classpath:hibernate.cfg.xml< / value>
< / property>
< property name =configurationClass>
< value> org.hibernate.cfg.AnnotationConfiguration< / value>
< / property>
< property name =hibernateProperties>
<道具>
< 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:注释驱动/>
< bean id =transactionManager
class =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
I am using Hibernate's lazy loading, and after adding OpenSessionInViewFilter to my web.xml I started to get sessionFactory missing exception, even after defining the sessionFactory bean to use.
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 seem to be the same problem but unfortunately I didn't understand the solution. I follow the suggested solution (move the sessionFactory to a file called root-spring.xml) i get the exception of the server startup.
Any idea what I am missing?
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>
Spring-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>
<!--<!– Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory –>-->
<mvc:resources mapping="/resources/**" location="/resources/"/>
</beans>
project structure:
Try moving the following configuration to 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>
这篇关于当使用OpenSessionInViewFilter时,获取没有名为“sessionFactory”的bean错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!