ComponentScan.basePackageClasses vs ComponentScan.basePackages to register a single Spring webMVC Controller?

后端 未结 1 1191
生来不讨喜
生来不讨喜 2020-12-29 15:10

I want to add a single specific controller class to my Spring WebApplicationContext. I ran across the following example: (its in Scala, but is adapted from here: using Compo

1条回答
  •  遥遥无期
    2020-12-29 15:46

    The full javadoc for that attribute reads

    Type-safe alternative to basePackages() for specifying the packages to scan for annotated components. The package of each class specified will be scanned.

    Consider creating a special no-op marker class or interface in each package that serves no purpose other than being referenced by this attribute.

    By type-safe, you can't make any mistakes with the String value of the name of the package. If you specify an incorrect class, it will fail at compile time.

    When you specify basePackageClasses, Spring will scan the package (and subpackages) of the classes you specify. This is a nice trick with no-op classes/interfaces like Controllers, Services, etc. Put all your controllers in one package containing the Controllers class and specify your Controllers class in the basePackageClasses. Spring will pick them all up.

    You still need to specify the filters.

    0 讨论(0)
提交回复
热议问题