Skip to content

Commit

Permalink
Configure JpaBaseConfiguration with custom ManagedClassNameFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
quaff committed Mar 1, 2024
1 parent 70769d9 commit 0c1976d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -46,6 +46,7 @@
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.ManagedClassNameFilter;
import org.springframework.orm.jpa.persistenceunit.PersistenceManagedTypes;
import org.springframework.orm.jpa.persistenceunit.PersistenceManagedTypesScanner;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
Expand All @@ -69,6 +70,7 @@
* @author Andy Wilkinson
* @author Kazuki Shimizu
* @author Eddú Meléndez
* @author Yanming Zhou
* @since 1.0.0
*/
@Configuration(proxyBeanMethods = false)
Expand Down Expand Up @@ -195,9 +197,11 @@ static class PersistenceManagedTypesConfiguration {
@Bean
@Primary
@ConditionalOnMissingBean
static PersistenceManagedTypes persistenceManagedTypes(BeanFactory beanFactory, ResourceLoader resourceLoader) {
static PersistenceManagedTypes persistenceManagedTypes(BeanFactory beanFactory, ResourceLoader resourceLoader,
ObjectProvider<ManagedClassNameFilter> managedClassNameFilter) {
String[] packagesToScan = getPackagesToScan(beanFactory);
return new PersistenceManagedTypesScanner(resourceLoader).scan(packagesToScan);
return new PersistenceManagedTypesScanner(resourceLoader, managedClassNameFilter.getIfAvailable())
.scan(packagesToScan);
}

private static String[] getPackagesToScan(BeanFactory beanFactory) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,6 +54,7 @@
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager;
import org.springframework.orm.jpa.persistenceunit.ManagedClassNameFilter;
import org.springframework.orm.jpa.persistenceunit.PersistenceManagedTypes;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter;
Expand All @@ -69,6 +70,7 @@
* @author Phillip Webb
* @author Dave Syer
* @author Stephane Nicoll
* @author Yanming Zhou
*/
abstract class AbstractJpaAutoConfigurationTests {

Expand Down Expand Up @@ -279,6 +281,16 @@ void customPersistenceUnitPostProcessors() {
});
}

@Test
void customManagedClassNameFilter() {
this.contextRunner.withBean(ManagedClassNameFilter.class, () -> (s) -> !s.endsWith("City"))
.withUserConfiguration(AutoConfigurePackageForCountry.class)
.run((context) -> {
EntityManager entityManager = context.getBean(EntityManagerFactory.class).createEntityManager();
assertThat(getManagedJavaTypes(entityManager)).contains(Country.class).doesNotContain(City.class);
});
}

private Class<?>[] getManagedJavaTypes(EntityManager entityManager) {
Set<ManagedType<?>> managedTypes = entityManager.getMetamodel().getManagedTypes();
return managedTypes.stream().map(ManagedType::getJavaType).toArray(Class<?>[]::new);
Expand Down Expand Up @@ -423,6 +435,12 @@ TransactionManager testTransactionManager() {

}

@Configuration(proxyBeanMethods = false)
@TestAutoConfigurationPackage(Country.class)
static class AutoConfigurePackageForCountry {

}

@Configuration(proxyBeanMethods = false)
@TestAutoConfigurationPackage(AbstractJpaAutoConfigurationTests.class)
static class TestConfigurationWithCustomPersistenceUnitManager {
Expand Down

0 comments on commit 0c1976d

Please sign in to comment.