Skip to content

Commit 805bb06

Browse files
committed
Merge pull request #39813 from quaff
* pr/39813: Configure JpaBaseConfiguration with custom ManagedClassNameFilter Closes gh-39813
2 parents 8c2b988 + a52ab77 commit 805bb06

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,7 @@
4646
import org.springframework.orm.jpa.JpaTransactionManager;
4747
import org.springframework.orm.jpa.JpaVendorAdapter;
4848
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
49+
import org.springframework.orm.jpa.persistenceunit.ManagedClassNameFilter;
4950
import org.springframework.orm.jpa.persistenceunit.PersistenceManagedTypes;
5051
import org.springframework.orm.jpa.persistenceunit.PersistenceManagedTypesScanner;
5152
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
@@ -69,6 +70,7 @@
6970
* @author Andy Wilkinson
7071
* @author Kazuki Shimizu
7172
* @author Eddú Meléndez
73+
* @author Yanming Zhou
7274
* @since 1.0.0
7375
*/
7476
@Configuration(proxyBeanMethods = false)
@@ -195,9 +197,11 @@ static class PersistenceManagedTypesConfiguration {
195197
@Bean
196198
@Primary
197199
@ConditionalOnMissingBean
198-
static PersistenceManagedTypes persistenceManagedTypes(BeanFactory beanFactory, ResourceLoader resourceLoader) {
200+
static PersistenceManagedTypes persistenceManagedTypes(BeanFactory beanFactory, ResourceLoader resourceLoader,
201+
ObjectProvider<ManagedClassNameFilter> managedClassNameFilter) {
199202
String[] packagesToScan = getPackagesToScan(beanFactory);
200-
return new PersistenceManagedTypesScanner(resourceLoader).scan(packagesToScan);
203+
return new PersistenceManagedTypesScanner(resourceLoader, managedClassNameFilter.getIfAvailable())
204+
.scan(packagesToScan);
201205
}
202206

203207
private static String[] getPackagesToScan(BeanFactory beanFactory) {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/AbstractJpaAutoConfigurationTests.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,6 +54,7 @@
5454
import org.springframework.orm.jpa.JpaVendorAdapter;
5555
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
5656
import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager;
57+
import org.springframework.orm.jpa.persistenceunit.ManagedClassNameFilter;
5758
import org.springframework.orm.jpa.persistenceunit.PersistenceManagedTypes;
5859
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
5960
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter;
@@ -69,6 +70,7 @@
6970
* @author Phillip Webb
7071
* @author Dave Syer
7172
* @author Stephane Nicoll
73+
* @author Yanming Zhou
7274
*/
7375
abstract class AbstractJpaAutoConfigurationTests {
7476

@@ -279,6 +281,16 @@ void customPersistenceUnitPostProcessors() {
279281
});
280282
}
281283

284+
@Test
285+
void customManagedClassNameFilter() {
286+
this.contextRunner.withBean(ManagedClassNameFilter.class, () -> (s) -> !s.endsWith("City"))
287+
.withUserConfiguration(AutoConfigurePackageForCountry.class)
288+
.run((context) -> {
289+
EntityManager entityManager = context.getBean(EntityManagerFactory.class).createEntityManager();
290+
assertThat(getManagedJavaTypes(entityManager)).contains(Country.class).doesNotContain(City.class);
291+
});
292+
}
293+
282294
private Class<?>[] getManagedJavaTypes(EntityManager entityManager) {
283295
Set<ManagedType<?>> managedTypes = entityManager.getMetamodel().getManagedTypes();
284296
return managedTypes.stream().map(ManagedType::getJavaType).toArray(Class<?>[]::new);
@@ -423,6 +435,12 @@ TransactionManager testTransactionManager() {
423435

424436
}
425437

438+
@Configuration(proxyBeanMethods = false)
439+
@TestAutoConfigurationPackage(Country.class)
440+
static class AutoConfigurePackageForCountry {
441+
442+
}
443+
426444
@Configuration(proxyBeanMethods = false)
427445
@TestAutoConfigurationPackage(AbstractJpaAutoConfigurationTests.class)
428446
static class TestConfigurationWithCustomPersistenceUnitManager {

0 commit comments

Comments
 (0)