Skip to content

Commit 1bd603f

Browse files
committed
Merge pull request #25443 from JohnNiang
* pr/25443: Polish "Allow to configure PersistenceUnitPostProcessor" Allow to configure PersistenceUnitPostProcessor Closes gh-25443
2 parents 67f10e5 + e1b1580 commit 1bd603f

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -21,6 +21,7 @@
2121
import java.util.UUID;
2222

2323
import javax.persistence.EntityManagerFactory;
24+
import javax.persistence.spi.PersistenceUnitInfo;
2425
import javax.sql.DataSource;
2526

2627
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
@@ -229,6 +230,19 @@ void customPersistenceUnitManager() {
229230
});
230231
}
231232

233+
@Test
234+
void customPersistenceUnitPostProcessors() {
235+
this.contextRunner.withUserConfiguration(TestConfigurationWithCustomPersistenceUnitPostProcessors.class)
236+
.run((context) -> {
237+
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = context
238+
.getBean(LocalContainerEntityManagerFactoryBean.class);
239+
PersistenceUnitInfo persistenceUnitInfo = entityManagerFactoryBean.getPersistenceUnitInfo();
240+
assertThat(persistenceUnitInfo).isNotNull();
241+
assertThat(persistenceUnitInfo.getManagedClassNames())
242+
.contains("customized.attribute.converter.class.name");
243+
});
244+
}
245+
232246
@Configuration(proxyBeanMethods = false)
233247
static class TestTwoDataSourcesConfiguration {
234248

@@ -388,6 +402,18 @@ PersistenceUnitManager persistenceUnitManager() {
388402

389403
}
390404

405+
@Configuration(proxyBeanMethods = false)
406+
@TestAutoConfigurationPackage(AbstractJpaAutoConfigurationTests.class)
407+
static class TestConfigurationWithCustomPersistenceUnitPostProcessors {
408+
409+
@Bean
410+
EntityManagerFactoryBuilderCustomizer entityManagerFactoryBuilderCustomizer() {
411+
return (builder) -> builder.setPersistenceUnitPostProcessors(
412+
(pui) -> pui.addManagedClassName("customized.attribute.converter.class.name"));
413+
}
414+
415+
}
416+
391417
@SuppressWarnings("serial")
392418
static class CustomJpaTransactionManager extends JpaTransactionManager {
393419

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -29,6 +29,7 @@
2929
import org.springframework.orm.jpa.JpaVendorAdapter;
3030
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
3131
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
32+
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor;
3233
import org.springframework.util.ClassUtils;
3334
import org.springframework.util.ObjectUtils;
3435
import org.springframework.util.StringUtils;
@@ -58,6 +59,8 @@ public class EntityManagerFactoryBuilder {
5859

5960
private AsyncTaskExecutor bootstrapExecutor;
6061

62+
private PersistenceUnitPostProcessor[] persistenceUnitPostProcessors;
63+
6164
/**
6265
* Create a new instance passing in the common pieces that will be shared if multiple
6366
* EntityManagerFactory instances are created.
@@ -104,6 +107,17 @@ public void setBootstrapExecutor(AsyncTaskExecutor bootstrapExecutor) {
104107
this.bootstrapExecutor = bootstrapExecutor;
105108
}
106109

110+
/**
111+
* Set the {@linkplain PersistenceUnitPostProcessor persistence unit post processors}
112+
* to be applied to the PersistenceUnitInfo used for creating the
113+
* {@link LocalContainerEntityManagerFactoryBean}.
114+
* @param persistenceUnitPostProcessors the persistence unit post processors to use
115+
* @since 2.5.0
116+
*/
117+
public void setPersistenceUnitPostProcessors(PersistenceUnitPostProcessor... persistenceUnitPostProcessors) {
118+
this.persistenceUnitPostProcessors = persistenceUnitPostProcessors;
119+
}
120+
107121
/**
108122
* A fluent builder for a LocalContainerEntityManagerFactoryBean.
109123
*/
@@ -232,6 +246,10 @@ public LocalContainerEntityManagerFactoryBean build() {
232246
if (EntityManagerFactoryBuilder.this.bootstrapExecutor != null) {
233247
entityManagerFactoryBean.setBootstrapExecutor(EntityManagerFactoryBuilder.this.bootstrapExecutor);
234248
}
249+
if (EntityManagerFactoryBuilder.this.persistenceUnitPostProcessors != null) {
250+
entityManagerFactoryBean.setPersistenceUnitPostProcessors(
251+
EntityManagerFactoryBuilder.this.persistenceUnitPostProcessors);
252+
}
235253
return entityManagerFactoryBean;
236254
}
237255

0 commit comments

Comments
 (0)