How to scan multiple paths using the @ComponentScan annotation?

componentscan multiple packages
spring component scan external jar
spring boot component scan multiple modules
spring boot component scan not working
spring boot disable component scan
context:component-scan not working
componentscan spring boot
componentscan(basepackages wildcard)

I'm using Spring 3.1 and bootstrapping an application using the @Configuration and @ComponentScan attributes.

The actual start is done with

new AnnotationConfigApplicationContext(MyRootConfigurationClass.class);

This Configuration class is annotated with

@Configuration
@ComponentScan("com.my.package")
public class MyRootConfigurationClass

and this works fine. However I'd like to be more specific about the packages I scan so I tried.

@Configuration
@ComponentScan("com.my.package.first,com.my.package.second")
public class MyRootConfigurationClass

However this fails with errors telling me it can't find components specified using the @Component annotation.

What is the correct way to do what I'm after?

Thanks

@ComponentScan uses string array, like this:

@ComponentScan({"com.my.package.first","com.my.package.second"})

When you provide multiple package names in only one string, Spring interprets this as one package name, and thus can't find it.

How to scan multiple paths using the @ComponentScan annotation?, This Configuration class is annotated with @Configuration @ComponentScan("​com.my.package") public class MyRootConfigurationClass. and this works fine. With Spring, we use the @ComponentScan annotation along with @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages. Let's say we have the following @Configuration in com.baeldung.componentscan.springapp package:

There is another type-safe alternative to specifying a base-package location as a String. See the API here, but I've also illustrated below:

@ComponentScan(basePackageClasses = {ExampleController.class, ExampleModel.class, ExmapleView.class})

Using the basePackageClasses specifier with your class references will tell Spring to scan those packages (just like the mentioned alternatives), but this method is both type-safe and adds IDE support for future refactoring -- a huge plus in my book.

Reading from the API, Spring suggests creating a no-op marker class or interface in each package you wish to scan that serves no other purpose than to be used as a reference for/by this attribute.

IMO, I don't like the marker-classes (but then again, they are pretty much just like the package-info classes) but the type safety, IDE support, and drastically reducing the number of base packages needed to include for this scan is, with out a doubt, a far better option.

Spring Component Scanning, With Spring, we use the @ComponentScan annotation along with @​Configuration annotation to specify the packages that we want to be scanned. annotation supports several flexible options for filtering the scanned classes  A delayed response but to give multiple packages using annotation based approach we can use as below: How to scan multiple paths using the @ComponentScan

Provide your package name separately, it requires a String[] for package names.

Instead of this:

@ComponentScan("com.my.package.first,com.my.package.second")

Use this:

@ComponentScan({"com.my.package.first","com.my.package.second"})

Spring Component Scan, Spring can auto scan, detect, and instantiate components from pre-defined project packages. Spring can auto scan all classes annotated with  The @ComponentScan annotation is used with the @Configuration annotation to tell Spring the packages to scan for annotated components. @ComponentScan is also used to specify base packages and base package classes using thebasePackageClasses or basePackages attributes of @ComponentScan.

Another way of doing this is using the basePackages field; which is a field inside ComponentScan annotation.

@ComponentScan(basePackages={"com.firstpackage","com.secondpackage"})

If you look into the ComponentScan annotation .class from the jar file you will see a basePackages field that takes in an array of Strings

public @interface ComponentScan {
String[] basePackages() default {};
}

Or you can mention the classes explicitly. Which takes in array of classes

Class<?>[]  basePackageClasses

Spring, Spring Boot, and Component Scan, Which packages does Spring Boot scan automatically? How do you define Component Scan with Spring Boot? How do you resolve problems involving Component Specific multiple packages: <context:component-scan  We can use @ComponentScan annotation to specify the base packages to scan. Any class which is annotated with @Component will be scanned for registered. Even classes annotated with @Configuration are candidates for component scanning as @Configuration annotation is meta-annotated with @Component.

You use ComponentScan to scan multiple packages using

@ComponentScan({"com.my.package.first","com.my.package.second"})

java How to scan multiple paths using the @ComponentScan , java How to scan multiple paths using the @ComponentScan annotation? @​ComponentScan(basePackages={"com.firstpackage","com.secondpackage"}) If you need to pull in that one class using annotations, add the following to one of your @Configuration classes @ComponentScan( basePackageClasses = YourClass.class, useDefaultFilters = false, includeFilters = { @ComponentScan.Filter(type = ASSIGNABLE_TYPE, value = YourClass.class) })

Scanning Multiple Paths Or Packages with @ComponentScan , A quick guide to how to scan or load multiple packages or paths in Spring Boot application with @ ComponentScan annotation along with  You have two options 1) Add the annotation or component scan 2) Move the component to a package already under component scan. What is the difference between @Component and @ComponentScan? @Component and @ComponentScan are for different purposes. @Component indicates that a class might be a candidate for creating a bean. Its like putting a hand up.

Spring @ComponentScan Annotation Example, Spring needs to know the packages to scan for annotated components. We can use @ComponentScan annotation to specify the base packages to scan. In our example, we have multiple packages defined, packageA,  In this code, the nested annotation @ ComponentScan. Filter is used to specify the filter type as FilterType. ASSIGNABLE_TYPE and the base class that should be excluded from scanning.. The output

Spring, Spring Boot and Component Scan – Spring Boot Tutorial, Why is Component Scan important? Which packages does Spring Boot do a Component Scan automatically? How do you define Component Scan with Spring Boot? or Specific Multiple Packages. <context:component-scan  Define @ComponentScan(“com.in28minutes.springboot”) This would scan the entire parent tree of com.in28minutes.springboot. Or define two specific Component Scans by using an array.

Comments
  • Two correct answers given at about the same time as far as I can tell. I'll give the accept to hage just because he has less points, but thank you both.
  • Could someone explain why @ComponentScan({"com.app", "com.controllers"}) doesn't work for me but @ComponentScan(basePackageClasses ={"com.controllers"}) does work nice ? I find it boring writing every class name
  • You only have to specify one class in the package, for the package you want to scan. This is known as a marker class. If you need to scan a package higher in the hierarchy that has no classes, spring suggests a technique using a "spring marker" interface or final class defined in that package solely for the purpose of package scanning.
  • Thanks after spending close to half an hour this was the missing dependency
  • Please consider adding explanation to your answer