1.SpringBootApplication啟動時會默認掃描主類當前包及子包,如果需要掃描主類當前包外的其他包或不掃描當前包下的特定包或類,可通過下列屬性實現:
Class<?>[] exclude() default {};
String[] excludeName() default {};
String[] scanBasePackages() default {};
Class<?>[] scanBasePackageClasses() default {};
詳細解釋請見org.springframework.boot.autoconfigure.SpringBootApplication,示例如下:
@SpringBootApplication(excludeName=("com.gxfw.wx.service.UserService"), scanBasePackages=("com.gxfw"))
excludeName屬性未經測試,此處權供參考
2.@EnableFeignClients注解默認也是會掃描注解所在包的當前包及子包,如果需要掃描其他包下的FeignClient,需要單獨使用屬性指定
String[] basePackages() default {};
Class<?>[] basePackageClasses() default {};
Class<?>[] defaultConfiguration() default {};
Class<?>[] clients() default {};
詳細解釋請見org.springframework.cloud.netflix.feign.EnableFeignClients,示例如下:
@EnableFeignClients(basePackages = ("com.gxfw"))
總結:@SpringBootApplicatoin是用的@ComponentScan掃描,掃描的是Component,包括@Component, @Controller, @Service, @Repository等,而@EnableFeignClients掃描的是@FeignClient,所以在指定掃描路徑時要分別指定,否則會報異常:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.gxfw.bbs.feign.ArticleFeignClient' available: expected at least 1 bean which qualifies as autowire candidate.
以及SpringBoot的啟動錯誤:
Description:
Field articleFeignClient in com.gxfw.bbs.wx.controller.ArticleAPI required a bean of type ‘com.gxfw.bbs.feign.ArticleFeignClient’ that could not be found.
Action:
Consider defining a bean of type ‘com.gxfw.bbs.feign.ArticleFeignClient’ in your configuration.