I have the following bean definition in a file called beans.xml :
<bean id="entityManager"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="mypackagename" />
<property name="persistenceProviderClass"
value="org.hibernate.jpa.HibernatePersistenceProvider" />
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<entry key="hibernate.hbm2ddl.auto" value="update" />
</map>
</property>
</bean>
And i can obtain this bean as an EntityManagerFactory
object with the following code:
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
EntityManagerFactory emf = context.getBean(EntityManagerFactory.class);
How is this possible?
As i see LocalContainerEntityManagerFactoryBean
does not implement EntityManagerFactory
.
How does this work exactly?
The chain is as follows: LocalContainerEntityManagerFactoryBean extends AbstractEntityManagerFactoryBean
which implements FactoryBean<EntityManagerFactory>
and thus you're getting your EntityManagerFactory
.
In fact you're requesting EntityManagerFactory
from the ApplicationContext
which already has EntityManagerFactory
that created by appropriate FactoryBean
.
Refer to FactoryBean documentation for more details.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.