Punter Vicky
Punter Vicky

Reputation: 17032

Spring - Excluding a bean from a package from being scanned

If I have about 50 spring beans in package com.xyz.abc and I want to exclude 2 of those beans from being treated as a bean , is there a way to do it? I am using Spring Boot.

@ComponentScan({'com.xyz.abc'})

There is a class Automobile.class which I don't want to be treated as Spring Bean. However I have Car.class which extends Automobile to be treated as spring bean.

Upvotes: 2

Views: 13291

Answers (1)

Tunaki
Tunaki

Reputation: 137229

You can exclude specific classes from being scanned into beans with the excludeFilters parameter of the @ComponentScan annotation.

This allows for the exclusion of specific classes, classes matching a given pattern...

For example, to exclude firstClass and secondClass you would write:

@ComponentScan(value = {'com.xyz.abc'}, excludeFilters = {
  @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = { firstClass.class, secondClass.class })
})

Upvotes: 11

Related Questions