javaspringannotations

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


Solution

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