5

How to exclude some beans from loading with component-scan, in an context that <import>'s from another xml that does need to scan all packages. This works nicly if I put it to main context:

<context:component-scan base-package="com.main">
        <context:exclude-filter expression="com.main.*Controller" type="regex"/>
</context:component-scan>

But I need controllers in live environment.

I'd like to exclude controller class loading from my integration test context. How would it be possible to achieve this?

Mihkel L.
  • 1,543
  • 1
  • 27
  • 42

1 Answers1

6

You could use spring profiles for that( reference How to set a Spring profile to a package? ), by using

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
        <!-- define profile beans at the end of the configuration file -->
    <beans profile="test">
    <context:component-scan base-package="com.main">
            <context:exclude-filter expression="com.main.*Controller" type="regex"/>
    </context:component-scan>
    </beans>

    <beans profile="!test">
    <context:component-scan base-package="com.main"/>
    </beans>

and annotating your test with the specific @ActiveProfile("test")

EDIT:

If your xml does not define a <component:scan> tag, the you can control package scanning from your unit test by using java configuration. The controllers can then be excluded with the @ComponentScan excludeFilter as follows:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class HelperTest {

    @Configuration
    @ComponentScan(basePackages = "yourPackage",
            excludeFilters = @ComponentScan.Filter(value = Controller.class, type = FilterType.ANNOTATION))
    @ImportResource(locations = "classpath:context.xml")
    static class TestConfiguration {


    }
Community
  • 1
  • 1
Alex Ciocan
  • 2,272
  • 14
  • 20
  • 1
    I wouldn't like to change the main context. I'd like to say what to exclude in my test context if possible. :) – Mihkel L. Dec 21 '16 at 09:54