ComponentScan excludeFilters在Spring 4.0.6.RELEASE中不起作用 [英] ComponentScan excludeFilters Not Working In Spring 4.0.6.RELEASE

查看:221
本文介绍了ComponentScan excludeFilters在Spring 4.0.6.RELEASE中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,我想在组件扫描时排除。我使用下面的代码来做到这一点,但似乎没有工作,虽然一切似乎都是正确的

I have a class which I want to exclude while component scanning. I am using the below code to do that but that doesn't seem to work although everything seems to be right

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

其实我想拥有ServiceImpl类,它实现了Service接口,正在我的rest api逻辑中使用,同时进行api的集成测试我想排除这个implmentation并加载模拟的实现。但这似乎并没有发生,因为即使使用上面我得到以下错误

Actually I want have "ServiceImpl" class, which implements "Service" interface, is being used in my rest api logic and while doing the integration test of an api I want to exclude this implmentation and load the mocked implementation. But this doesn't seem to happen as even after using the above I am getting the below error

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

我也花了大部分时间都没有,但没有任何作用。

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

感谢任何帮助。

推荐答案

经过大量的工作和研究,我注意到Spring在组件扫描方面的行为并不奇怪。

After lot of work and research I noticed that Spring's behavior is little weird in term of component scanning.

工件是这样的:

ServiceImpl是实现服务接口的真正实现类。
ServiceMockImpl是实现服务界面的模拟植入类。

"ServiceImpl" is the real implementation class which implements "Service" interface. "ServiceMockImpl" is the mocked implantation class which implements "Service" interface.

我想调整组件扫描,以便它只加载ServiceMockImpl但不是ServiceImpl。

I wanted to tweak the component scanning so that it only loads the "ServiceMockImpl" but not "ServiceImpl".

我必须在@中添加@ ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value = ServiceImpl.class) ComponentScan测试配置类,从组件扫描中排除该特定类。但是即使在完成上述更改并且测试失败之后,这两个类都被加载了。

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

经过大量的工作和研究,我发现ServiceImpl已经被加载了,因为另一个正在加载的类,并且在其中包含所有包的@ComponentScan。所以我添加了代码以从组件扫描中排除Application类,如下所示@ ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value = Application.class)。

After lot of work and research I found that the "ServiceImpl" was getting loaded because of the other class which was getting loaded and has "@ComponentScan" for all packages on top of in it. So I added the code to exclude the "Application" class from the component scanning as follows "@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Application.class)".

之后它按预期工作。

下面的代码

@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 have seen that lot of questions on component scanning are unanswered for long hence I thought to add these details as it may help somebody in the future.

HTH .. 。

这篇关于ComponentScan excludeFilters在Spring 4.0.6.RELEASE中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆