If you are not using the entityManagerFactory()
method anywhere in your java configuration, you can instead return the LocalContainerEntityManagerFactoryBean
object.
The LocalContainerEntityManagerFactoryBean
is both an InitializingBean
and a FactoryBean
. These are special interfaces that Spring can use to initialize a bean and then add it to the context.
You could therefore change your method to
@Bean(name = "entityManagerFactory", destroyMethod = "close")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setPackagesToScan("com.bignibou.domain");
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPersistenceProvider(persistenceProvider());
entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter());
entityManagerFactoryBean.setJpaPropertyMap(propertiesMap());
return entityManagerFactoryBean;
}
Spring will take care of calling afterPropertiesSet()
and getObject()
on the object returned by the method and adding the created EntityManagerFactory
bean to the context.
This is detailed in the IoC chapter of the Spring documentation.