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