4

Is it possible to override/deactivate a component-scan definition defined in a "legacy" context definition xml?

Let's say I have a context definition example-context.xmlin a dependency of my project and I import that context in my own project in a context definition my-context.xml

example-context.xml contains a component-scan definition:

 <context:component-scan base-package="com.foo"/>

Is it possible to somehow deactivate or override that component-scan definition by adding some exclusion filters to it?

SRy
  • 2,901
  • 8
  • 36
  • 57
Cristi
  • 190
  • 2
  • 10

2 Answers2

3

If you are importing a context with <context:component-scan declaration in it, then there is no way to override it, the only workaround will be to not import the child context. You should be able to selectively move the specific beans you require to another context and import that instead.

Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
2

There are exclude filters.

But I personally think that component scan is an evil.

Some examples:

Exclude subpackages from Spring autowiring?

How do <context:include-filter> and <context:exclude-filter> work in Spring?

Community
  • 1
  • 1
kan
  • 28,279
  • 7
  • 71
  • 101
  • I definitely agree on the component scan being evil, but; I would selectively agree with this. IMHO `@Component` declarations should only ever be used in the end application or its primary service libraries and not within any common library. Spring's inherent move towards no configuration service loading with boot is really gritting with me. – Brett Ryan Aug 10 '16 at 05:50