Skip to content

Commit b183309

Browse files
committed
Stop a BeanPostProcessor from preventing config of packages to scan
Previously, if a BeanPostProcessor bean was declared in a configuration class that depended on the persistence context, packages to scan would not be configured on the LocalContainerEntityManagerFactoryBean. This was due to the BeanPostProcessor bean triggering the early instantiation of the LCEMFB before EntityScanBeanPostProcessor, the BeanPostProcessor, that applies the @EntityScan configuration, had been given a chance to configure it. This commit updates EntityScanBeanPostProcessor to implement Ordered with an order of zero. This ensures that its ordering is predictable and that it will be driven before any unordered BeanPostProcessor. This means that, unless the user has specifically ordered their BeanPostProcessor to run before EntityScanBeanPostProcessor, LCEMFB’s packages to scan will be configured as expected. Fixes gh-2478
1 parent 1035e5b commit b183309

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
2929
import org.springframework.beans.factory.support.GenericBeanDefinition;
3030
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
31+
import org.springframework.core.Ordered;
3132
import org.springframework.core.annotation.AnnotationAttributes;
3233
import org.springframework.core.type.AnnotationMetadata;
3334
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@@ -91,7 +92,7 @@ private String[] getPackagesToScan(AnnotationMetadata metadata) {
9192
* on an {@link EntityScan} annotation.
9293
*/
9394
static class EntityScanBeanPostProcessor implements BeanPostProcessor,
94-
SmartInitializingSingleton {
95+
SmartInitializingSingleton, Ordered {
9596

9697
private final String[] packagesToScan;
9798

@@ -125,6 +126,11 @@ public void afterSingletonsInstantiated() {
125126
+ "ensure an appropriate bean is registered.");
126127
}
127128

129+
@Override
130+
public int getOrder() {
131+
return 0;
132+
}
133+
128134
}
129135

130136
}

spring-boot/src/test/java/org/springframework/boot/orm/jpa/EntityScanTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import org.junit.Rule;
2323
import org.junit.Test;
2424
import org.junit.rules.ExpectedException;
25+
import org.springframework.beans.BeansException;
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.beans.factory.config.BeanPostProcessor;
2528
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2629
import org.springframework.context.annotation.Bean;
2730
import org.springframework.context.annotation.Configuration;
@@ -99,6 +102,13 @@ public void needsEntityManageFactory() throws Exception {
99102
this.context = new AnnotationConfigApplicationContext(MissingEntityManager.class);
100103
}
101104

105+
@Test
106+
public void userDeclaredBeanPostProcessorWithEntityManagerDependencyDoesNotPreventConfigurationOfPackagesToScan() {
107+
this.context = new AnnotationConfigApplicationContext(
108+
BeanPostProcessorConfiguration.class, BaseConfig.class);
109+
assertSetPackagesToScan("com.mycorp.entity");
110+
}
111+
102112
private void assertSetPackagesToScan(String... expected) {
103113
String[] actual = this.context.getBean(
104114
TestLocalContainerEntityManagerFactoryBean.class).getPackagesToScan();
@@ -148,6 +158,33 @@ static class BasePackagesAndBasePackageClasses extends BaseConfig {
148158
static class MissingEntityManager {
149159
}
150160

161+
@Configuration
162+
@EntityScan("com.mycorp.entity")
163+
static class BeanPostProcessorConfiguration {
164+
165+
@Autowired
166+
private EntityManagerFactory entityManagerFactory;
167+
168+
@Bean
169+
public BeanPostProcessor beanPostProcessor() {
170+
return new BeanPostProcessor() {
171+
172+
@Override
173+
public Object postProcessBeforeInitialization(Object bean, String beanName)
174+
throws BeansException {
175+
return bean;
176+
}
177+
178+
@Override
179+
public Object postProcessAfterInitialization(Object bean, String beanName)
180+
throws BeansException {
181+
return bean;
182+
}
183+
};
184+
185+
}
186+
}
187+
151188
private static class TestLocalContainerEntityManagerFactoryBean extends
152189
LocalContainerEntityManagerFactoryBean {
153190

0 commit comments

Comments
 (0)