首先是在spring boot项目中注入的jar包中的接口
@Controller
public class MessageTransferStationController {
@Autowired
private MessageClient messageClient;
}
其次,jar包中的MessageClient的源码
public interface MessageClient {
}
MessageClient分别有三个实现类
public class CoreMessageClient implements MessageClient {}
@Component
public class SpringMessageClientImpl extends CoreMessageClient implements MessageClient {}
public class Spring2MessageClientImpl extends CoreMessageClient implements MessageClient {}
当我在spring boot项目中注入MessageClient接口的时候,启动报错:
017-06-12 15:06:53.281 WARN 9780 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'messageTransferStationController': Unsatisfied dependency expressed through field 'messageClient'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.sinosoft.messageclient.MessageClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2017-06-12 15:06:53.283 INFO 9780 --- [ main] o.apache.catalina.core.StandardService : Stopping service Tomcat
2017-06-12 15:06:53.316 INFO 9780 --- [ main] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-06-12 15:06:53.431 ERROR 9780 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
APPLICATION FAILED TO START
Description:
Field messageClient in com.sinosoft.oa.message_transfer_station.web.MessageTransferStationController required a bean of type 'com.sinosoft.messageclient.MessageClient' that could not be found.
Action:
Consider defining a bean of type 'com.sinosoft.messageclient.MessageClient' in your configuration.
Process finished with exit code 1
请问各位大佬,为什么MessageClient为什么不能注入,我猜想过MessageClient的实现类没有被Spring管理,我看了jar包中源码有一个实现类加上了@Component注解
求教各位大佬,这是什么原因引起的?
注:引用jar包中的其它接口也测试过了,同样的错误,也就是说引用的这个jar包中的所有接口都没有被spring管理,或者没有扫描。
多次实验后,发现引进来的jar包没有被spring管理,即时jar包中有spring注解。
在主启动类上加入@ComponentScan("/"),问题得到了解决
在主启动方法上添加注解就可以了, 我是这么解决的:
@ComponentScan("com.aaa.coupons.service")
public class CouponsApplication extends WebMvcConfigurerAdapter{
}
项目A包明为com.gs.aaa,项目B包名为com.gs.bbb,项目A依赖B,把A项目启动APP移动到com.gs包下(以前在com.gs.aaa下)即可正常扫描B项目中的注解
纠正一下楼上的几个答案
用@ComponentScan的确能解决注入失败的问题,但如果注入的是feign接口(例如像做接口抽离到一个统一jar中去)
应该在主启动类上加@EnableFeignClients(basePackages = "你抽离的jar包的接口所在包目录")
亲测可用
我引用的是一个maven里面的接口,报这个错误
@楼主 我有和你一样的问题,加上@ComponentScan("/") 还是不行
//注入
@Autowired
//@Qualifier("DefaultFastFileStorageClient")
protected FastFileStorageClient ffsClient;
// 接口
public interface FastFileStorageClient extends GenerateStorageClient {}
//实现类
@Component
public class DefaultFastFileStorageClient extends DefaultGenerateStorageClient implements FastFileStorageClient {}
报错::
Description:
Field ffsClient in com.eharmony.iams.api.collect.ImageCollectController required a bean of type 'com.github.tobato.fastdfs.service.FastFileStorageClient' that could not be found.
Action:
Consider defining a bean of type 'com.github.tobato.fastdfs.service.FastFileStorageClient' in your configuration.
我也碰到这个问题了,怎么解决的啊?
这个问题我也遇到了,原因是因为Spring boot自动扫描注解只能扫描启动类所在的包及其子包,而通常情况下外部引用的jar包都不会在启动类的子包中,所以无法扫描到。如果是自己打包的jar包,可以考虑修改jar包的包名,将打包的jar包改成启动类的子包。其它包我也没想到好办法,如果有好办法麻烦告诉我一下。