Vijender Kumar
Vijender Kumar

Reputation: 1475

How to exclude @Configuration class from component-scan in testContext.xml

I have a class AppConfig annotated with @Configuration which is having various bean definitions which includes beans which performs a third party hit from the application. now in my spring integration test cases, I do not want these beans to be initialized. that is where I created an another bean with name TestAppConfig annotated with @Configuration where I have mocked all those beans which performs third party hit. now in my testContext.xml I am adding an exclude filter to context:component-scan where I am specifying the AppConfig package to be excluded. but somehow this AppConfig is being initialized everytime. I have tried proper regex, but still things are not working. if anyone know the cause then please share.

Upvotes: 0

Views: 1157

Answers (1)

Nur Zico
Nur Zico

Reputation: 2447

After see the comment that you are using spring 3.2, you can see here for older version of spring to use @Profile

You can use @Profile annotation to determine which @Bean will be created or not.

Example:

Defined Beans are below

@Bean
@Profile("production")
    public DataSource prodDataSource() {
        ...
        return dataSource;
    }

@Bean
@Profile("development")
    public DataSource devDataSource() {
        ...
        return dataSource;
    }

For profile called "development"

  1. In app.properties spring.profiles.active=development
  2. prodDataSource will not be called. But devDataSource will be called

For profile called "production"

  1. In app.properties spring.profiles.active=production
  2. prodDataSource will not be called. But devDataSource will be called

Upvotes: 2

Related Questions