Skip to content

Commit 9ca396e

Browse files
committed
Remove scanned class only when conflicting with imported class
Closes gh-36835 (cherry picked from commit 121c0ac)
1 parent 2eb0ba3 commit 9ca396e

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,11 @@ protected void processConfigurationClass(ConfigurationClass configClass, Predica
258258
return;
259259
}
260260
else if (configClass.isScanned()) {
261-
String beanName = configClass.getBeanName();
262-
if (StringUtils.hasLength(beanName) && this.registry.containsBeanDefinition(beanName)) {
263-
this.registry.removeBeanDefinition(beanName);
261+
if (existingClass.isImported()) {
262+
String beanName = configClass.getBeanName();
263+
if (StringUtils.hasLength(beanName) && this.registry.containsBeanDefinition(beanName)) {
264+
this.registry.removeBeanDefinition(beanName);
265+
}
264266
}
265267
// An implicitly scanned bean definition should not override an explicit import.
266268
return;

spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationIntegrationTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import example.scannable.CustomStereotype;
2727
import example.scannable.DefaultNamedComponent;
2828
import example.scannable.FooService;
29+
import example.scannable.FooServiceImpl;
2930
import example.scannable.MessageBean;
3031
import example.scannable.ScopedProxyTestBean;
3132
import example.scannable_implicitbasepackage.ComponentScanAnnotatedConfigWithImplicitBasePackage;
@@ -43,6 +44,7 @@
4344
import org.springframework.beans.factory.config.BeanDefinition;
4445
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
4546
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
47+
import org.springframework.beans.factory.support.RootBeanDefinition;
4648
import org.springframework.context.ApplicationContext;
4749
import org.springframework.context.EnvironmentAware;
4850
import org.springframework.context.ResourceLoaderAware;
@@ -84,6 +86,17 @@ void controlScan() {
8486
assertContextContainsBean(ctx, "fooServiceImpl");
8587
}
8688

89+
@Test
90+
void controlScanWithExplicitRegistration() {
91+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
92+
ctx.registerBeanDefinition("myFooService", new RootBeanDefinition(FooServiceImpl.class));
93+
ctx.scan(example.scannable.PackageMarker.class.getPackage().getName());
94+
ctx.refresh();
95+
96+
assertContextContainsBean(ctx, "myFooService");
97+
assertContextContainsBean(ctx, "fooServiceImpl");
98+
}
99+
87100
@Test
88101
void viaContextRegistration() {
89102
ApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanAnnotatedConfig.class);

0 commit comments

Comments
 (0)