How to scan multiple paths using @ComponentScan annotation?

I am using Spring 3.1 and loading the application using the @Configuration and @ComponentScan .

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 it works great. However, I would like to clarify which 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 that they cannot find the components specified using the @Component annotation.

What is the right way to do what I need?

thank

+68
java spring annotations
May 29 '12 at 7:09 a.m.
source share
6 answers

@ComponentScan uses a string array, like this:

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

When you provide multiple package names in only one line, Spring interprets this as a single package name and therefore cannot find it.

+118
May 29 '12 at 7:21
source share

There is another type- safe alternative to locating the base package as String. Check out the API here , but I also illustrated below:

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

Using the basePackageClasses specifier with your class references, tell Spring to scan these packages (just like the alternatives mentioned), but this method is safe in type 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 every package that you want to scan, which serves no other purpose than to use as a reference for / using this attribute.

IMO, I don’t like marker classes (but again, they are pretty much like packet-information classes), but type safety, IDE support and a significant reduction in the number of basic packages that need to be enabled for this scan are, without a doubt, much better option.

+37
Jan 24 '14 at 12:55
source share

Indicate the name of your package separately; String[] is required 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"}) 
+17
May 29 '12 at 7:23
source share

Another way to do this is to use the basePackages field; which is the field inside the ComponentScan annotation.

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

If you look in the annotation of ComponentScan.class from the jar file, you will see the basePackages field, which takes an array of strings

 public @interface ComponentScan { String[] basePackages() default {}; } 
+9
Feb 10 '16 at 14:19
source share

You are using ComponentScan to scan multiple packets with

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

+3
Nov 24 '16 at 7:07
source share

make sure you add this dependency to your pom.xml

 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> 
0
Feb 01 '19 at 10:20
source share



All Articles