sof
sof

Reputation: 9649

NoSuchBeanDefinitionException caused by nested bean definition for Spring JPA

Spring JPA 4.2.1

Nested bean definition looks like below but gets NoSuchBeanDefinitionException:

"No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined"

<bean id="tm" class="...JpaTransactionManager">
    <property name="entityManagerFactory">
        <bean class="...LocalContainerEntityManagerFactoryBean">
            <property name="dataSource">
                <bean class="...BasicDataSource"
                    p:driverClassName="..." p:url="...">
                </bean>
            </property>
        </bean>
    </property>
</bean>

Only the flat definition works below, why?

<bean id="tm" class="...JpaTransactionManager">
    <property name="entityManagerFactory" ref="emf" />
</bean>
<bean id="emf" class="...LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="ds" />
</bean>
<bean id="ds" class="...BasicDataSource"
    p:driverClassName="..." p:url="...">
</bean>

Upvotes: 1

Views: 96

Answers (1)

Roman
Roman

Reputation: 6646

Some other bean also requires EntityManagerFactory (which one, I can't say - you didn't show the full stack trace). And inner beans are anonymous and they can't be retrieved using BeanFactory.getBean() or @Autowired, that's why you get this error in the first case.

Inner beans are always anonymous and they are always created with the outer bean. It is not possible to inject inner beans into collaborating beans other than into the enclosing bean.

In the second case EntityManagerFactoryBean is not an inner bean and can be used by any other bean.

Upvotes: 1

Related Questions