How to scan multiple paths using the @ComponentScan annotation?

前端 未结 8 1051
执笔经年
执笔经年 2020-11-29 17:38

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

The actual start is done wit

8条回答
  •  [愿得一人]
    2020-11-29 18:00

    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.

提交回复
热议问题