Home > other >  @ComponentScan is not applied - all Java packages are scanned
@ComponentScan is not applied - all Java packages are scanned

Time:07-07

I have a Spring Boot app with several dependencies:

@ComponentScan({"com.test.adapter",
                "com.dependency.commons.framework.util"}
)

But during startup I see that call packages from dependency jar are applied and I get conflicts. Do you know how I can apply only Java package: "com.dependency.commons.framework.util"?

CodePudding user response:

Solution A

Use the attribute excludeFilters of @ComponentScan.

@ComponentScan(basePackages = { "com.test.adapter",
                "com.dependency.commons.framework.util" },
    excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.example.ignore.packages.*"))

Solution B

As your title of the question is

@ComponentScan is not applied - all Java packages are scanned

Probably your launcher class that has the @SpringBootApplication is on a upper level of a package. Spring will automatically scan all the classes that are on that level of where this class is placed and all nested subpackages.

See the following structure as an example

enter image description here

What you can do here to avoid that is to move the launcher class inside a specific package outside of the root package

enter image description here

  • Related