springboot须要加载jar包里的bean时,通常是使用注解@ComponentScan(basePackages = {"com.test.http", "com.test.client"})来实现,但@ComponentScan在使用时有些注意事项须要当心java
因为须要用到springcloud feign作RPC调用,须要注入client的api,所以我加了@ComponentScan扫描client所在的包路径,结果在启动时报错:spring
Description:api
Parameter 0 of method indicatorServiceApi in com.client.ServiceApiConfig required a bean of type 'feign.codec.Decoder' that could not be found.springboot
反序列类找不到可以使用对象,而后我就加了个FeignConfig加载默认的feign配置ui
@Configuration @Import(FeignClientsConfiguration.class) public class FeignConfig { }
再次启动依然报同一个错误!this
从现象来看,client.ServiceApiConfig至少是在FeignConfig以前初始化的,为什么不是先初始化本地么?这个还真不是spa
回头看看@ComponentScan的注释:code
* <p>Either {@link #basePackageClasses} or {@link #basePackages} (or its alias * {@link #value}) may be specified to define specific packages to scan. If specific * packages are not defined, scanning will occur from the package of the * class that declares this annotation.
意思是若是没有定义packages,就从启动类的包路径开始。这就引含了一个意思:若是有定义,启动类的包路径并不会自动添加进去!对象
再来看看上面的问题,原来启动类的包路径跟api的包不同,致使spring只扫描了api的包路径,固然就找不到FeignConfig配置啦!将自身的包路径加到@ComponentScan里便可。ip
PS:@ComponentScan的包顺序不受影响的哦〜〜