Spring boot version: [2.1.6.RELEASE, 2.2.1.RELEASE]
package a.b;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Foo {
public static void main(String[] args) {
new SpringApplicationBuilder(Foo.class)
.run(args);
}
}
package a.c;
import org.springframework.stereotype.Service;
@Service
public class Foo {}
application.properties
contains following property
spring.main.allow-bean-definition-overriding=false
Expectation
Failure on startup because two beans with name foo
are not supposed to be registered.
Reality
No exception and silently the non-main class bean is left out. (i.e. bean with name foo
is registered with class a.b.Foo
)
Note: This is only the case when another bean contributor is the entry-point Spring boot application class.
Comment From: wilkinsona
Thanks for raising an issue, @JigarJoshi. From our discussion on Gitter:
Could you open a Spring Framework issue for that please?
ClassPathBeanDefinitionScanner
considers the bean definitions fora.b.Foo
anda.c.Foo
to be compatible so it keeps the first one (a.b.Foo
) and ignores the second one. That doesn't seem right to me.
Comment From: snicoll
I can't reproduce the issue. Besides the example above would not work given that a.c
is not going to be a target of classpath scanning with a Spring Boot app in a.b
.
Here is the exception I get:
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'foo' for bean class [com.a.b.Foo] conflicts with existing, non-compatible bean definition of same name and class [com.a.Foo]
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:361) ~[spring-context-6.1.1-SNAPSHOT.jar:6.1.1-SNAPSHOT]
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:288) ~[spring-context-6.1.1-SNAPSHOT.jar:6.1.1-SNAPSHOT]
at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:128) ~[spring-context-6.1.1-SNAPSHOT.jar:6.1.1-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:296) ~[spring-context-6.1.1-SNAPSHOT.jar:6.1.1-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-6.1.1-SNAPSHOT.jar:6.1.1-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:196) ~[spring-context-6.1.1-SNAPSHOT.jar:6.1.1-SNAPSHOT]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:164) ~[spring-context-6.1.1-SNAPSHOT.jar:6.1.1-SNAPSHOT]
... 11 common frames omitted
Can you please move that code snippet into an actual project we can run? You can attach a zip to this issue or push the code to a GitHub repository.
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.