Reputation: 485
Background: I am working in a project named com.x.myproject
. I have the dependency of two other packages com.x.document
and com.x.library
. both packages have the same class with name QueueHelper
.
Now, In my project, I have to scan one other package com.x.security
which internally scans com.x
, something like that:
@SpringBootApplication
@ComponentScan(basePackages = {"com.x"})
@EnableCaching
public class Security {
.......
}
in com.x.myproject
@SpringBootApplication
@ComponentScan(basePackages = {"com.x.myproject","com.x.security"}, excludeFilters={
@ComponentScan.Filter(type=FilterType.REGEX, pattern="com.x.document.*"),
@ComponentScan.Filter(type=FilterType.REGEX, pattern="com.x.library.*")})
public class MyProject{
.......
}
It all works fine when I use excludefilters
in com.x.security
but I want to use it in com.x.myproject
The exception which I got is
Annotation-specified bean name 'queueHelper' for bean class [com.x.library.utils.QueueHelper] conflicts with existing, non-compatible bean definition of same name and class [com.x.document.utils.QueueHelper]
Upvotes: 1
Views: 6902
Reputation: 2730
Three answers come to my mind:
Give a different names to com.x.library.utils.QueueHelper
and com.x.document.utils.QueueHelper
via the @Component
annotation. Spring will use the simple class name by default for naming the beans. You can annotate one with @Component('libraryQueueHelper')
and the other with @Component('documentQueueHelper')
. However, now you'll have to give a @Qualifier(<name>)
in each place you're autowiring these beans.
Exclude them in your module, like you do in your question and then change their names using @Bean
annotated methods within a @Configuration
. When using in the third module, you'll need to use a @Qualifier
to autowire the correct bean.
Rename the classes. This is the best solution in this case, but since you asked this question, I guess it's not viable.
Upvotes: 5