【问题标题】:Spring + Hibernate + Maven NoSuchBeanDefinitionExceptionSpring + Hibernate + Maven NoSuchBeanDefinitionException
【发布时间】:2014-10-17 21:23:32
【问题描述】:

这个问题是Spring + Hibernate + Maven org.hibernate.MappingException: AnnotationConfiguration instance is required 的演变。

我目前的日志是:

 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

下面的配置文件是:

<?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>

似乎问题总是出在创建 DAO bean 上。在我的 config.xml 中添加:default-autowire="byName" 后会发生这种情况。然而之前还有另一个错误:

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

【问题讨论】:

    标签: java spring hibernate spring-mvc autowired


    【解决方案1】:

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

      <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>
    

    改变后

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

    【讨论】:

      猜你喜欢
      • 2016-10-15
      • 1970-01-01
      • 2013-10-02
      • 2018-01-22
      • 2012-12-27
      • 2011-07-19
      • 2016-04-27
      • 2012-06-22
      • 1970-01-01
      相关资源
      最近更新 更多