筑基期 2017-06-12 07:08 采纳率: 0%
浏览 26702

spring boot注入jar包中的接口总是提示that could not be found

首先是在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管理,或者没有扫描。

  • 写回答

9条回答 默认 最新

  • 筑基期 2017-06-13 01:43
    关注

    多次实验后,发现引进来的jar包没有被spring管理,即时jar包中有spring注解。
    在主启动类上加入@ComponentScan("/"),问题得到了解决

    评论

报告相同问题?

悬赏问题

  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码