Java 无法解析对 bean 'sessionFactory' 的引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24549378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot resolve reference to bean 'sessionFactory'
提问by vipin cp
Iam working with spring , hibernate maven project am getting the exception Cannot resolve reference to bean 'sessionFactory' on dispatcher servlet.xml file. Below iam adding the three file contents. both dispatcherservlet and application context are in resources folder. what is the problem? please help
我正在使用 spring,hibernate maven 项目收到异常无法解析对调度程序 servlet.xml 文件上的 bean 'sessionFactory' 的引用。下面我添加了三个文件内容。dispatcherservlet 和应用程序上下文都在资源文件夹中。问题是什么?请帮忙
My error:org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stockBo' defined in class path resource [dispatcher-servlet.xml]: Cannot resolve reference to bean 'stockDao' while setting bean property 'stockDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stockDao' defined in class path resource [dispatcher-servlet.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined
我的错误:org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [dispatcher-servlet.xml] 中定义名称为“stockBo”的 bean 创建时出错:设置 bean 属性“stockDao”时无法解析对 bean“stockDao”的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [dispatcher-servlet.xml] 中定义名称为“stockDao”的 bean 创建时出错:设置 bean 属性“sessionFactory”时无法解析对 bean“sessionFactory”的引用;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为“sessionFactory”的 bean
Dispatcher-servlet.xml file
Dispatcher-servlet.xml 文件
<!-- Stock Data Access Object -->
<bean id="stockBo" lazy-init="true" class="com.org.rolltickets.stock.bo.impl.StockBoImpl" >
<property name="stockDao">
<ref local="stockDao" />
</property>
</bean>
<bean id="stockDao" lazy-init="true" class="com.org.rolltickets.stock.dao.impl.StockDaoImpl" >
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
Applcaitioncontext.xml
应用上下文.xml
<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>/hibernate/Stock.hbm.xml</value>
</list>
</property>
</bean>
Web.xml file
Web.xml 文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
采纳答案by JamesENL
Move your config inside your dispatcher-servlet.xml into another xml file called root-servlet.xml (the name doesn't actually matter, what matters is that you move it to a new file).
将您的 dispatcher-servlet.xml 中的配置移动到另一个名为 root-servlet.xml 的 xml 文件中(名称实际上并不重要,重要的是您将其移动到一个新文件中)。
Leave the dispatcher-servlet.xml file blank.
将 dispatcher-servlet.xml 文件留空。
Then inside your web.xml add the new XML file after you list your ApplicationContext.xml
然后在你的 web.xml 中,在你列出 ApplicationContext.xml 之后添加新的 XML 文件
It should look like this:
它应该是这样的:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext.xml</param-value>
<param-value>classpath:root-servlet.xml</param-value>
<context-param>
What is happening, is that the servlet is running the code inside your dispatcher-servlet.xml
before its running your ApplicationContext.xml
file.
发生的事情是,servletdispatcher-servlet.xml
在运行您的ApplicationContext.xml
文件之前正在运行您内部的代码。
So the dataSource
bean doesn't exist yet. By running ApplciationContext.xml first, you will create the bean, so it will be available.
所以这个dataSource
bean 还不存在。通过首先运行 ApplciationContext.xml,您将创建 bean,因此它将可用。