Spring + Hibernate + Maven NoSuchBeanDefinitionException

时间:2014-10-17 21:23:33

标签: java spring hibernate spring-mvc autowired

这个问题是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。添加后会发生这种情况:default-autowire =&#34; byName&#34;在我的config.xml中。然而之前又出现了另一个错误:

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

1 个答案:

答案 0 :(得分:0)

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