Using @ComponentScan or with only one class

后端 未结 4 586
鱼传尺愫
鱼传尺愫 2020-12-06 03:56

I\'m maintaining a project with two set of main packages, the project is using Spring and Spring MVC, one of these packages contains several controllers and is scanned using

4条回答
  •  醉酒成梦
    2020-12-06 05:00

    In addition to the method described by Emerson Farrugia there is a less verbose solution which has been supported since Spring Framework 4.2 as mentioned in the documentation here.

    As of Spring Framework 4.2, @Import also supports references regular component classes, analogous to the AnnotationConfigApplicationContext.register method. This is particularly useful if you want to avoid component scanning, by using a few configuration classes as entry points to explicitly define all your components.

    So your example would simply become:

    @Import(YourClass.class)
    

提交回复
热议问题