Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scan multiple paths using the @ComponentScan annotation?

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

like image 640
Programming Guy Avatar asked May 29 '12 07:05

Programming Guy


People also ask

What does @ComponentScan annotation do?

Using @ComponentScan in a Spring Application. With Spring, we use the @ComponentScan annotation along with the @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.

How do I use component scans with multiple packages?

To add many packages to Component Scan, you should pass the String[] array to the @ComponentScan annotation.

What is @ComponentScan annotation in Spring?

The @ComponentScan annotation is used with the @Configuration annotation to tell Spring the packages to scan for annotated components. @ComponentScan also used to specify base packages and base package classes using thebasePackageClasses or basePackages attributes of @ComponentScan.

What is @component and @ComponentScan?

@Component and @ComponentScan are for different purposes. @Component indicates that a class might be a candidate for creating a bean. It's like putting a hand up. @ComponentScan is searching packages for Components.


2 Answers

@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.

like image 91
hage Avatar answered Sep 30 '22 09:09

hage


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.

like image 22
Prancer Avatar answered Sep 30 '22 09:09

Prancer