I have this spring-boot application using spring boot 3.2.1 and can start it.
@SpringBootApplication
@EnableJpaRepositories(queryLookupStrategy = Key.USE_DECLARED_QUERY)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
However, when I create a second spring-boot application in my project in the same package (e.g. for testing), I cannot start any of the spring-boot applications anymore. So e.g. I create a copy Application2 and start any of the two then I get this error:
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'fooReposiotry', defined in com.customer.component.data.FooReposiotry defined in @EnableJpaRepositories declared on Application, could not be registered.
A bean with that name has already been defined in com.customer.component.data.FooReposiotry defined in @EnableJpaRepositories declared on Application2 and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
I just added some spaces and a newline to the original output from spring-boot to make it more readable and obvious what is going wrong.
I once used this approach in a spring-boot 2.x application and there it worked AFAIK.
IMHO the problem is that @SpringBootApplication is annotated with @SpringBootConfiguration what is annotated with @Configuration. This results in the second application being considered as a @Configuration by spring-boot.
My point of view is that this is some kind of bug. Assuming I have several @SpringBootTests and only one or a subset of them needs some slightly different configuration, I would expect being able to create an additional TestApplication extending Application that adds these aspects. However when the TestApplication is setup it will also scan and find Application causing duplicate beans.
In that scenario it may also be questionable if one @Configuration class extends another that spring-boot will then load the parent and the child class what can only result in duplicated beans as those beans defined in the parent class will also appear in the child class (unless overloaded and replaced).
Comment From: hohwille
I found a workaround:
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = { Application.class }))
public class TestApplication extends Application { ... }
Still it would be great if spring-boot would do this automatically as anything else can never work.
Comment From: wilkinsona
Thanks for the report.
I am doubtful that this has ever worked as the behavior of component scanning hasn't changed, as far as I can remember. If you have multiple @Configuration classes in packages covered by component scanning they should all be found.
That said, it sounds like this used to work for you and I'd like to understand how that was the case. To that end, please provide a minimal example that works with Spring Boot 2.x and fails as you have described above with 3.2.1.
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.