@ -25,11 +25,8 @@ import jakarta.persistence.EntityManagerFactory;
import org.apache.commons.logging.Log ;
import org.apache.commons.logging.LogFactory ;
import org.springframework.beans.BeansException ;
import org.springframework.beans.factory.BeanFactory ;
import org.springframework.beans.factory.BeanFactoryAware ;
import org.springframework.beans.factory.ObjectProvider ;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory ;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages ;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration ;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass ;
@ -45,9 +42,12 @@ import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.context.annotation.Primary ;
import org.springframework.core.io.ResourceLoader ;
import org.springframework.orm.jpa.JpaTransactionManager ;
import org.springframework.orm.jpa.JpaVendorAdapter ;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean ;
import org.springframework.orm.jpa.persistenceunit.PersistenceManagedTypes ;
import org.springframework.orm.jpa.persistenceunit.PersistenceManagedTypesScanner ;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager ;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter ;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor ;
@ -73,7 +73,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
* /
@Configuration ( proxyBeanMethods = false )
@EnableConfigurationProperties ( JpaProperties . class )
public abstract class JpaBaseConfiguration implements BeanFactoryAware {
public abstract class JpaBaseConfiguration {
private final DataSource dataSource ;
@ -81,8 +81,6 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware {
private final JtaTransactionManager jtaTransactionManager ;
private ConfigurableListableBeanFactory beanFactory ;
protected JpaBaseConfiguration ( DataSource dataSource , JpaProperties properties ,
ObjectProvider < JtaTransactionManager > jtaTransactionManager ) {
this . dataSource = dataSource ;
@ -128,11 +126,12 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware {
@Bean
@Primary
@ConditionalOnMissingBean ( { LocalContainerEntityManagerFactoryBean . class , EntityManagerFactory . class } )
public LocalContainerEntityManagerFactoryBean entityManagerFactory ( EntityManagerFactoryBuilder factoryBuilder ) {
public LocalContainerEntityManagerFactoryBean entityManagerFactory ( EntityManagerFactoryBuilder factoryBuilder ,
PersistenceManagedTypes persistenceManagedTypes ) {
Map < String , Object > vendorProperties = getVendorProperties ( ) ;
customizeVendorProperties ( vendorProperties ) ;
return factoryBuilder . dataSource ( this . dataSource ) . packages( getPackagesToScan ( ) ) . properties ( vendorProperti es)
. mappingResources( getMappingResources ( ) ) . jta ( isJta ( ) ) . build ( ) ;
return factoryBuilder . dataSource ( this . dataSource ) . managedTypes( persistenceManagedTyp es)
. properties( vendorProperties ) . mappingResources( getMappingResources ( ) ) . jta ( isJta ( ) ) . build ( ) ;
}
protected abstract AbstractJpaVendorAdapter createJpaVendorAdapter ( ) ;
@ -147,14 +146,6 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware {
protected void customizeVendorProperties ( Map < String , Object > vendorProperties ) {
}
protected String [ ] getPackagesToScan ( ) {
List < String > packages = EntityScanPackages . get ( this . beanFactory ) . getPackageNames ( ) ;
if ( packages . isEmpty ( ) & & AutoConfigurationPackages . has ( this . beanFactory ) ) {
packages = AutoConfigurationPackages . get ( this . beanFactory ) ;
}
return StringUtils . toStringArray ( packages ) ;
}
private String [ ] getMappingResources ( ) {
List < String > mappingResources = this . properties . getMappingResources ( ) ;
return ( ! ObjectUtils . isEmpty ( mappingResources ) ? StringUtils . toStringArray ( mappingResources ) : null ) ;
@ -192,9 +183,26 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware {
return this . dataSource ;
}
@Override
public void setBeanFactory ( BeanFactory beanFactory ) throws BeansException {
this . beanFactory = ( ConfigurableListableBeanFactory ) beanFactory ;
@Configuration ( proxyBeanMethods = false )
@ConditionalOnMissingBean ( { LocalContainerEntityManagerFactoryBean . class , EntityManagerFactory . class } )
static class PersistenceManagedTypesConfiguration {
@Bean
@Primary
@ConditionalOnMissingBean
PersistenceManagedTypes persistenceManagedTypes ( BeanFactory beanFactory , ResourceLoader resourceLoader ) {
String [ ] packagesToScan = getPackagesToScan ( beanFactory ) ;
return new PersistenceManagedTypesScanner ( resourceLoader ) . scan ( packagesToScan ) ;
}
private static String [ ] getPackagesToScan ( BeanFactory beanFactory ) {
List < String > packages = EntityScanPackages . get ( beanFactory ) . getPackageNames ( ) ;
if ( packages . isEmpty ( ) & & AutoConfigurationPackages . has ( beanFactory ) ) {
packages = AutoConfigurationPackages . get ( beanFactory ) ;
}
return StringUtils . toStringArray ( packages ) ;
}
}
@Configuration ( proxyBeanMethods = false )