'Exclude @SpringBootApplication from component scanning

I have two classes marked as @SpringBootApplication under one directory:

@SpringBootApplication
public class FirstSpringBootApplication 

and

@SpringBootApplication
public class SecondSpringBootApplication

@SpringBootApplication annotation contains @ComponentScan annotation and @EnableAutoConfiguration annotation. So, each of two of these classes will consider another as @Configuration bean. How to exclude FirstSpringBoodApplication from component scanning by SecondSpringBootApplication without using profiles?



Solution 1:[1]

In case you need to define two or more excludeFilters criteria, you have to use the array.

For instances in this section of code I want to exclude all the classes in the org.xxx.yyy package and another specific class, MyClassToExclude

Solution 2:[2]

The annotate class with below annotations will work similarly as @SpringBootApplication. It also does the same, and the excludeFilter is important, which is used to specify which class not to include while scanning.

@EnableAutoConfiguration
@ComponentScan(excludeFilters={@Filter(type=CUSTOM, classes={TypeExcludeFilter.class})})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Sae1962