SoFunction
Updated on 2025-03-05

The problem of deleting beans or not loading beans in SpringBoot project is solved

Use the @ tag in the @ComponentScan annotation to not load.

@ComponentScan(excludeFilters = {@(type = , pattern ={"Bail Name"})})
@ComponentScan(excludeFilters  = {@(type = FilterType.ASSIGNABLE_TYPE,classes = Class Name.class)})

Implement the BeanFactoryPostProcessor interface and delete it after compilation (of course, you can also write a configuration class here)

@SpringBootApplication
public class EmpServiceApplication implements BeanFactoryPostProcessor {
    public static void main(String[] args) {
        (, args);
    }
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
                // Check if it is a BeanDefinitionRegistry                if (beanFactory instanceof BeanDefinitionRegistry) {
                    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
                    // Get the names of all beans                    String[] beanNames = ();
                    for (String beanName : beanNames) {
                        // Get the definition of bean                        BeanDefinition beanDefinition = (beanName);
                        // Get the class name of the bean                       String beanClassName = ();
                        // Custom exclusion logic                        if (beanClassName != null && ("Bail Name")) {
                            // Remove unwanted beans                            (beanName);
                            ("Excluded bean: " + beanName);
                        }}} 
                else {
            throw new IllegalStateException("BeanFactory is not a BeanDefinitionRegistry");
        }
    }
}

Use @ComponentScan, and use custom filters to implement the TypeFilter interface, specifying that some beans are not compiled and not loaded

@SpringBootApplication
@ComponentScan(excludeFilters = @(
    // Use custom filters    type = , 
    // Specify a custom filter class    classes = ))
public class ServiceApplication{
    public static void main(String[] args) {
        (, args);
    }
  }
}
import ;
import ;
import ;
/**
  * @Description: Custom exclusion filter: Implements custom exclusion logic, returns true to exclude the class, and returns false to include the class.
  * @Version: 1.0
  **/
public class CustomExcludeFilter implements TypeFilter {
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) {
        // Implement custom exclusion logic here.  For example, decide whether to exclude the class based on the name, package name, or other attributes of the class.  Here is the fully qualified name of the class.  Please note the version upgrade        String className = ().getClassName();
        if (className != null && ("Bail Name")) {
            // Return true to exclude the class.            return true;
        }
        // Return false to indicate that the class is included.        return false;
    }
}

This is the article about deleting beans or not loading beans in SpringBoot project. For more related content related to SpringBoot project that does not load beans, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!