springboot項目踩坑

1.SpringBootApplication啓動時會默認掃描主類當前包及子包,如果需要掃描主類當前包外的其他包或不掃描當前包下的特定包或類,可通過下列屬性實現:

@SpringBootApplication(scanBasePackages = {"xxx", "xxx"})

2.@EnableFeignClients註解默認也是會掃描註解所在包的當前包及子包,如果需要掃描其他包下的FeignClient,需要單獨使用屬性指定

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

3  在SpringBoot中集成MyBatis,可以在mapper接口上添加@Mapper註解,將mapper注入到Spring,但是如果每一給mapper都添加@mapper註解會很麻煩,這時可以使用@MapperScan註解來掃描包。

@MapperScan(basePackages = "com.yuewei.core.mapper")

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章