org.springframework.beans.factory.NoSuchBeanDefinitionException

There are no available Samebug tips for this exception. Do you have an idea how to solve this issue? A short tip would help users who saw this issue last week.

  • *Bottom line*: When EntityManagerFactoryUtils.findEntityManagerFactory() is called with a persistence unit name, it looks for an EntityManagerFactory bean through the whole context hierarchy. If the persistence unit name is omitted, it will only look at the current context and ignore beans in any parent context. *Why this matters*: I have a Spring MVC application that has a global context in /WEB-INF/applicationContext.xml and an MVC context in /WEB-INF/dispatcher-servlet.xml. The EntityManagerFactory is configured in the global context, and we have an OpenEntityManagerInViewInterceptor in the MVC context. If I leave the OEMIVInterceptor's persistenceUnitName property undefined, the application fails to start up with this error: {noformat} org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0 at org.springframework.beans.factory.BeanFactoryUtils.beanOfType(BeanFactoryUtils.java:394) at org.springframework.orm.jpa.EntityManagerFactoryUtils.findEntityManagerFactory(EntityManagerFactoryUtils.java:111) at org.springframework.orm.jpa.EntityManagerFactoryAccessor.setBeanFactory(EntityManagerFactoryAccessor.java:139) at ... {noformat} However, if I specify the persistenceUnitName, all is well. This also worked previously when all the beans were defined in one context (without the need to specify the persistence unit). If this is not a bug, I fail to see the logic behind it. Here is the relevant snippet from EntityManagerFactoryUtils.findEntityManagerFactory(): {code} if (StringUtils.hasLength(unitName)) { // See whether we can find an EntityManagerFactory with matching persistence unit name. String[] candidateNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, EntityManagerFactory.class); // ... } else { return BeanFactoryUtils.beanOfType(beanFactory, EntityManagerFactory.class); } {code} As you can see, it calls the "...IncludingAncestors" method if the persistence unit name is defined, but uses the no-hierarchy version otherwise. This could easily be fixed by changing the second call to: {code} else { return BeanFactoryUtils.beanOfTypeIncludingAncestors(beanFactory, EntityManagerFactory.class); } {code}
    via by Tim Yates,
  • *Bottom line*: When EntityManagerFactoryUtils.findEntityManagerFactory() is called with a persistence unit name, it looks for an EntityManagerFactory bean through the whole context hierarchy. If the persistence unit name is omitted, it will only look at the current context and ignore beans in any parent context. *Why this matters*: I have a Spring MVC application that has a global context in /WEB-INF/applicationContext.xml and an MVC context in /WEB-INF/dispatcher-servlet.xml. The EntityManagerFactory is configured in the global context, and we have an OpenEntityManagerInViewInterceptor in the MVC context. If I leave the OEMIVInterceptor's persistenceUnitName property undefined, the application fails to start up with this error: {noformat} org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0 at org.springframework.beans.factory.BeanFactoryUtils.beanOfType(BeanFactoryUtils.java:394) at org.springframework.orm.jpa.EntityManagerFactoryUtils.findEntityManagerFactory(EntityManagerFactoryUtils.java:111) at org.springframework.orm.jpa.EntityManagerFactoryAccessor.setBeanFactory(EntityManagerFactoryAccessor.java:139) at ... {noformat} However, if I specify the persistenceUnitName, all is well. This also worked previously when all the beans were defined in one context (without the need to specify the persistence unit). If this is not a bug, I fail to see the logic behind it. Here is the relevant snippet from EntityManagerFactoryUtils.findEntityManagerFactory(): {code} if (StringUtils.hasLength(unitName)) { // See whether we can find an EntityManagerFactory with matching persistence unit name. String[] candidateNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, EntityManagerFactory.class); // ... } else { return BeanFactoryUtils.beanOfType(beanFactory, EntityManagerFactory.class); } {code} As you can see, it calls the "...IncludingAncestors" method if the persistence unit name is defined, but uses the no-hierarchy version otherwise. This could easily be fixed by changing the second call to: {code} else { return BeanFactoryUtils.beanOfTypeIncludingAncestors(beanFactory, EntityManagerFactory.class); } {code}
    via by Tim Yates,
  • Bernhard's Weblog
    via by Unknown author,
  • Planet NetBeans
    via by Unknown author,
    • org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0 at org.springframework.beans.factory.BeanFactoryUtils.beanOfType(BeanFactoryUtils.java:394) at org.springframework.orm.jpa.EntityManagerFactoryUtils.findEntityManagerFactory(EntityManagerFactoryUtils.java:111) at org.springframework.orm.jpa.EntityManagerFactoryAccessor.setBeanFactory(EntityManagerFactoryAccessor.java:139)
    No Bugmate found.