Spring + Hibernate + Maven NoSuchBeanDefinitionException [英] Spring + Hibernate + Maven NoSuchBeanDefinitionException

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

问题描述

这个问题是一个演化的春天+休眠+ Maven的 org.hibernate.MappingException:需要AnnotationConfiguration实例



我当前的日志是:

  log4j:WARN没有appender可以找到记录器(org.springframework.core.env.StandardEnvironment)。 
log4j:WARN请正确初始化log4j系统。
线程main中的异常org.springframework.beans.factory.BeanCreationException:在类路径资源[config.xml]中定义名称为'client'的Bean时出错:无法在设置bean时解析对bean'ClientDAOx'的引用财产'clientDAO';嵌套的例外是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为 'ClientDAOx' 豆在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:336)
定义
在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1456)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
org.springfra上的
mework.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)
在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
。在组织.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework INIT> .context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)$ b $ LT b。在org.springframework.context.support.ClassPathXmlApplicationContext&(ClassPathXmlApplicationContext.java:139)
。在org.springframework。上下文。 support.ClassPathXmlApplicationContext。< init>(ClassPathXmlApplicationContext.java:83)
at ee.st.running.aktorstask.Main.main(Main.java:17)
导致:org.springframework.beans .factory.NoSuchBeanDefinitionException:没有名为 'ClientDAOx' 豆在org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition定义
(DefaultListableBeanFactory.java:641)在org.springframework.beans.factory
.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1157)
在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:280)
在org.springframework.beans.factory .support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
... 15个

以下配置文件是:

 &升t;?xml version =1.0encoding =UTF-8?> 
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns :p =http://www.springframework.org/schema/p
xmlns:aop =http://www.springframework.org/schema/aopxmlns:context =http:// www.springframework.org/schema/context
xmlns:jee =http://www.springframework.org/schema/jeexmlns: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/sche ma / tx / spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
默认自动装配=绰号>

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

< tx:annotation-driven />

< context:annotation-config />

< bean id =dataSourceclass =org.apache.commons.dbcp.BasicDataSourcedestroy-method =close>
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =usernamevalue =admin/>
< property name =passwordvalue =root/>
< / bean>


< bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource>< / property>
< property name =annotatedClasses>
< list>
< value> ee.st.running.model.Client< /值>
<值> ee.st.running.model.Order< /值>
< / list>
< / property>

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

< / property>
< / bean>

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

< 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 =orderclass =ee.st.running.model.Order>
< property name =orderDAOref =OrderDAO>< / property>
< / bean>

< bean id =orderDAOclass =ee.st.running.dao.OrderDAOImpl>
< property name =sessionFactoryref =sessionFactory>< / property>
< / bean>

< / beans>

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

 创建名为'clientDAOImpl'的bean时出错[C:\Users\ Странник\workspace\aktorstask\target\classes\ee\st\running\dao\ClientDAOImpl.class]:调用init方法失败;嵌套异常是java.lang.IllegalArgumentException:'sessionFactory'或'hibernateTemplate'是必需的


解决方案

您的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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆