简体   繁体   English

无法解析对bean'sessionFactory'的引用

[英]Cannot resolve reference to bean 'sessionFactory'

Iam working with spring , hibernate maven project am getting the exception Cannot resolve reference to bean 'sessionFactory' on dispatcher servlet.xml file. 我与spring一起工作,休眠的maven项目遇到异常无法解析对调度程序servlet.xml文件的bean'sessionFactory'的引用。 Below iam adding the three file contents. 在iam下面添加三个文件内容。 both dispatcherservlet and application context are in resources folder. dispatcherservlet和应用程序上下文都在resources文件夹中。 what is the problem? 问题是什么? please help 请帮忙

My error:org.springframework.web.util.NestedServletException: Request processing failed; 我的错误:org.springframework.web.util.NestedServletException:请求处理失败; 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'; 嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建在类路径资源[dispatcher-servlet.xml]中定义的名称为'stockBo'的bean时出错:设置bean属性'stockDao'时无法解析对bean'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'; 嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建在类路径资源[dispatcher-servlet.xml]中定义的名称为'stockDao'的bean时出错:在设置bean属性'sessionFactory'时无法解析对bean'sessionFactory'的引用; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined 嵌套的异常是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 Applcaitioncontext.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>

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 然后,在列出ApplicationContext.xml之后,在web.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. 发生的情况是,该servlet在运行ApplicationContext.xml文件之前正在运行dispatcher-servlet.xml内部的代码。

So the dataSource bean doesn't exist yet. 因此, dataSource bean还不存在。 By running ApplciationContext.xml first, you will create the bean, so it will be available. 通过首先运行ApplciationContext.xml,您将创建Bean,使其可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring-data-cassandra:创建名为“sessionFactory”的bean时出错,并且无法解析对bean“cassandraTemplate”的引用 - Spring-data-cassandra: Error creating bean with name 'sessionFactory' and Cannot resolve reference to bean 'cassandraTemplate' 无法解析对bean MapExecutionContextDao的引用 - Cannot resolve reference to bean MapExecutionContextDao 无法解析对 bean &#39;cacheManager&#39; 的引用 - Cannot resolve reference to bean 'cacheManager' Spring SAML实现无法解析对bean的引用 - Spring SAML implementation cannot resolve reference to bean 无法解析对 bean“jpaSharedEM_entityManagerFactory”的引用 - Cannot resolve reference to bean 'jpaSharedEM_entityManagerFactory' AspectJ 无法解析对 bean“selectAll”的引用 - AspectJ Cannot resolve reference to bean 'selectAll' Spring Security .DaoAuthenticationProvider:无法解析对bean的引用 - Spring Security .DaoAuthenticationProvider: Cannot resolve reference to bean Red5无法解析bean引用 - Red5 cannot resolve bean reference 无法解析对 bean &#39;neo4jTemplate&#39; 的引用 - Cannot resolve reference to bean 'neo4jTemplate' sessionFactory的bean中的name属性在IDEA中为“红色”,即无法解析属性 - name attribute in bean for sessionFactory is in 'red' in IDEA i.e. cannot resolve property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM