NoSuchBeanDefinitionException

NoSuchBeanDefinitionException

本文介绍了Spring + Hibernate + Maven NoSuchBeanDefinitionException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是一个演化的春天+休眠+ Maven的。



我当前的日志是:

  

您的bean id是 clientDAO ,并且您将ref指定为 ClientDAO 。它应该区分大小写

 < bean id =clientclass =ee.st.running.model.Client > 
< property name =clientDAOref =ClientDAO>< / property> //这是问题。
< / bean>

< bean id =clientDAOclass =ee.st.running.dao.ClientDAOImpl>
< property name =sessionFactoryref =sessionFactory>< / property>
< / bean>

更改后

 < bean id =clientclass =ee.st.running.model.Client> 
< property name =clientDAOref =ClientDAO>< / property> //这是问题。
< / bean>


This question is an evolution of Spring + Hibernate + Maven org.hibernate.MappingException: AnnotationConfiguration instance is required that.

My current log is:

 log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException:     Error creating bean with name 'client' defined in class path resource [config.xml]: Cannot resolve reference to bean 'ClientDAOx' while setting bean property 'clientDAO'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'ClientDAOx' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:336)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1456)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at ee.st.running.aktorstask.Main.main(Main.java:17)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'ClientDAOx' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:641)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1157)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:280)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
... 15 more

And the following config file is:

<?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:p="http://www.springframework.org/schema/p"
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:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"
default-autowire="byName">

  <context:component-scan base-package=".*" />

 <tx:annotation-driven/>

  <context:annotation-config />

  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/sqlconnection" />
    <property name="username" value="admin" />
    <property name="password" value="root" />
  </bean>


  <bean id="sessionFactory"     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
       <property name="dataSource" ref="dataSource"></property>
     <property name="annotatedClasses">
            <list>
                    <value>ee.st.running.model.Client</value>
                    <value>ee.st.running.model.Order</value>
            </list>
        </property>

    <property name="hibernateProperties">
      <props>
        <prop
         key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        <prop key="hibernate.show_sql">true</prop>
      </props>

    </property>
  </bean>

  <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
    p:sessionFactory-ref="sessionFactory">
  </bean>

  <bean id = "client" class = "ee.st.running.model.Client">
  <property name="clientDAO" ref = "ClientDAO"></property>
  </bean>

  <bean id = "clientDAO" class = "ee.st.running.dao.ClientDAOImpl">
  <property name="sessionFactory" ref = "sessionFactory"></property>
  </bean>


  <bean id = "order" class = "ee.st.running.model.Order">
  <property name="orderDAO" ref = "OrderDAO"></property>
  </bean>

      <bean id = "orderDAO" class = "ee.st.running.dao.OrderDAOImpl">
  <property name="sessionFactory" ref = "sessionFactory"></property>
  </bean>

  </beans>

It seems that the problem is always in creating a DAO bean. That occurs after adding: default-autowire="byName" in my config.xml. Yet previously there was another error:

Error creating bean with name 'clientDAOImpl' defined in file [C:\Users\Странник\workspace\aktorstask\target\classes\ee\st\running\dao\ClientDAOImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
解决方案

Your bean id is clientDAO and you are giving ref as ClientDAO. It should be case sensitive

  <bean id = "client" class = "ee.st.running.model.Client">
  <property name="clientDAO" ref = "ClientDAO"></property> //Here is the problem.
  </bean>

  <bean id = "clientDAO" class = "ee.st.running.dao.ClientDAOImpl">
  <property name="sessionFactory" ref = "sessionFactory"></property>
  </bean>

After changing

  <bean id = "client" class = "ee.st.running.model.Client">
  <property name="clientDAO" ref = "ClientDAO"></property> //Here is the problem.
  </bean>

这篇关于Spring + Hibernate + Maven NoSuchBeanDefinitionException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 06:15