ComponentScan excludeFilters not working in Spring 4.0.6.RELEASE

I have a class that I want to exclude when scanning components. I use the code below for this, but it does not work, although everything seems to be correct.

@ComponentScan(basePackages = { "common", "adapter", "admin"}, excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ServiceImpl.class) }) 

Actually, I want the "ServiceImpl" class, which implements the "Service" interface, to be used in my api logic to relax, and, by performing the api integration test, I want to exclude this implication and load the confusing implementation. But this is not like even after using the above, I get the error below

 No qualifying bean of type [admin.Service] is defined: expected single matching bean but found 2: ServiceMockImpl,ServiceImpl 

I spent too much time on this, but nothing works.

Any help is appreciated.

+6
source share
3 answers

After a lot of work and research, I noticed that Spring's behavior is a little strange in terms of component scanning.

The artifacts were as follows:

"ServiceImpl" is a real implementation class that implements the "Service" interface. "ServiceMockImpl" is a mock implantation class that implements the "Service" interface.

I would like to configure component scanning so that it only loads "ServiceMockImpl", but not "ServiceImpl".

I had to add "@ ComponentScan.Filter (type = FilterType.ASSIGNABLE_TYPE, value = ServiceImpl.class)" to the "@ComponentScan" class of the test configuration to exclude this particular class from component scanning. But both classes were loaded even after the above changes were made, and the tests failed.

After a lot of work and research, I found that "ServiceImpl" was loading due to another class that loaded and had "@ComponentScan" for all the packages on top of it. So I added code to exclude the Application class from component scanning as follows: "@ ComponentScan.Filter (type = FilterType.ASSIGNABLE_TYPE, value = Application.class)."

After that, he worked as expected.

Code as below

 @ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = OAuthCacheServiceImpl.class), @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Application.class) }, basePackages = { "common", "adapter", "admin"}) 

I saw that many questions on scanning components have remained unanswered for a long time, so I thought to add this data, as this may help someone in the future.

NTN ...

+21
source

First, thank you very much for the answers @Yuriy and @ripudam, but what scares me is when my excludeFilters contain Configuration.class. I need to use @Import to import Classe, which was annotated using @ Configuration.I when I use excludeFilters = {@Filter (type = FilterType.ANNOTATION, value {EnableWebMvc.class, Controller.class}), everything works fine. ContextLoaderListener does not register the controller first.

for instance

 //@Import(value = { SecurityConfig.class, MethodSecurityConfig.class, RedisCacheConfig.class, QuartzConfig.class }) @ComponentScan(basePackages = { "cn.myself" }, excludeFilters = { @Filter(type = FilterType.ANNOTATION,value={EnableWebMvc.class,Controller.class}) }) public class RootConfig { } 
+1
source

I had a problem when using @Configuration , @EnableAutoConfiguration and @ComponentScan when trying to exclude specific configuration classes. The thing is, it does not work!

In the end, I solved the problem using @SpringBootApplication , which according to the Spring documentation does the same functionality as the three above in one annotation.

Another tip is to try first without specifying your package check (without the basePackages filter).

 @SpringBootApplication(exclude= {Foo.class}) public class MySpringConfiguration {} 
0
source

Source: https://habr.com/ru/post/974483/


All Articles