简体   繁体   English

设置bean属性'dataSource'时,无法解析对bean'DataSource'的引用.factory.NoSuchBeanDefinitionException:

[英]Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource' .factory.NoSuchBeanDefinitionException:

This is the bean with this id defined in my spring-servlet.xml file 这是在我的spring-servlet.xml文件中定义了此id的bean

<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}" />

This is the complete stacktrace 这是完整的堆栈跟踪

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [META-INF/spring/hibernate-context.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)

this is my application context file defined in my WEB-INF folder 这是我的WEB-INF文件夹中定义的应用程序上下文文件

<?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:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
</beans>

this is my complete web.xml file 这是我完整的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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>YummyFoods</display-name>
  <welcome-file-list>
    <welcome-file>/JSP/welcome.jsp</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <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>
  <servlet>
    <servlet-name>imageServlet</servlet-name>
    <servlet-class>com.yummyfoods.spring.servlet.ImageServlet</servlet-class>
</servlet>
  <servlet-mapping>
    <servlet-name>imageServlet</servlet-name>
    <url-pattern>/image/*</url-pattern>
</servlet-mapping>
<error-page>
    <error-code>404</error-code>
    <location>/error</location>
</error-page>
</web-app>

this is my spring-servlet.xml file 这是我的spring-servlet.xml文件

<mvc:resources location="/resources/" mapping="/resources/**"/>
    <mvc:default-servlet-handler/>          
    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/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="classpath: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.LocalSessionFactoryBean">
        <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.connection.SetBigStringTryClob">true</prop>
                <prop key="hibernate.jdbc.batch_size">0</prop>
            </props>
        </property>
    </bean>

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

        <!-- one of the properties available; the maximum file size in bytes -->
        <property name="maxUploadSize" value="10000000" />
    </bean>

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

Please assist me find my way. 请帮助我找到自己的方式。

From the look of things you are defining a dataSource in a DispatcherServlet context configuration file ie spring-servlet.xml as you have specified. 从您看来,您在DispatcherServlet上下文配置文件中定义了一个dataSource,即您指定的spring-servlet.xml And the entityManagerFactory in a root application context file. 根应用程序上下文文件中的entityManagerFactory Beans in the root application context cannot reference those in the servlet application context file. 根应用程序上下文中的Bean无法引用servlet应用程序上下文文件中的Bean。 You need to move your dataSource bean to the hibernate-context.xml file. 您需要将dataSource bean移动到hibernate-context.xml文件。

What comes with your dependecies? 您的家属带来了什么? I think you should include your datasource def in applicationContext.xml and then the hibernate thing should be happy. 我认为你应该在applicationContext.xml包含你的数据源def,然后hibernate的东西应该很开心。

So... the 所以

<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.LocalSessionFactoryBean">
         <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.connection.SetBigStringTryClob">true</prop>
                 <prop key="hibernate.jdbc.batch_size">0</prop>
             </props>
         </property>
     </bean>
 <tx:annotation-driven />
     <bean id="transactionManager"
         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory" />
     </bean>

goes in applicationContext.xml and is removed from spring-servlet.xml applicationContext.xml ,从spring-servlet.xml删除

Then the hibernate-cfg.xml wich is embeded in the Session factory def should find it's data source and be happy at loading. 然后hibernate-cfg.xml嵌入在Session工厂中,应该找到它的数据源,并且在加载时很开心。

It's better to isolate servlet mapping and mvc definitions from back end stuff (bean factory etc...) as it is easier to change it in the future if needed. 最好将servlet映射和mvc定义与后端内容(bean工厂等)隔离开来,因为如果需要,将来更容易更改它。

With what do you build your app? 你用什么构建你的应用程序?

暂无
暂无

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

相关问题 设置 bean 属性“mongoOperations”时无法解析对 bean“mongoTemplate”的引用 - Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations' 设置bean属性'userDetailsS​​ervice'时无法解析对bean的引用 - Cannot resolve reference to bean while setting bean property 'userDetailsService' 设置 bean 属性“mongoOperations”时无法解析对 bean“mongoTemplate”的引用 - Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations 创建名称为“ mongoTransactionManager”的bean时出错:设置bean属性“ datastore”时无法解析对bean“ mongoDatastore”的引用 - Error creating bean with name 'mongoTransactionManager': Cannot resolve reference to bean 'mongoDatastore' while setting bean property 'datastore' 无法解析对bean&#39;dataSource&#39;的Spring3 + Struts 2的引用 - Cannot resolve reference to bean 'dataSource' Spring3 + Struts 2 在部署 AWS Lambda 时设置 bean 属性“mongoOperations”时无法解析对 bean“mongoTemplate”的引用 - Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations' while deploying AWS Lambda Spring 引导 + Kotlin + MongoDb:设置 bean 属性时无法解析对 bean 'mongoTemplate' 的引用 - Spring boot + Kotlin + MongoDb: Cannot resolve reference to bean 'mongoTemplate' while setting bean property 使用键[0]设置bean属性“触发”时,无法解析对bean“ cronTrigger”的引用 - Cannot resolve reference to bean 'cronTrigger' while setting bean property 'triggers' with key [0] 使用键[0]设置bean属性“ sourceList”时,无法解析对bean&#39;org.springframework.security.web.DefaultSecurityFilterChain#0&#39;的引用 - cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0] 创建名称为&#39;org.springframework.security.filterChains的bean时出错,设置bean属性&#39;sourceList&#39;时无法解析对bean的引用 - Error creating bean with name 'org.springframework.security.filterChains Cannot resolve reference to bean while setting bean property 'sourceList'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM