简体   繁体   中英

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.

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 })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM