首页 > 解决方案 > 错误 - 将一个服务的依赖项注入另一个服务控制器时,一个“包”类型的 bean [Spring-Boot]

问题描述

我有一个 jar 库,其中一个服务声明如下所示

@org.springframework.stereotype.Service
public class ABC {

现在我已经在不同的服务中使用 maven pom.xml 导入了这个 jar 文件,我的代码看起来像这样

    import a.b.ABC


    @RestController
    @RequestMapping("")
    @Slf4j
    public class xyz {
    @Autowired
    private ABC abcd;

使用 mvn clean install 编译时,这是错误,我得到了

{“json_mssg”:{“date”:“21-07-2021 20:29:06.653”,“thread”:“main”,“level”:“ERROR”,“log”:“ newline____newline newline__应用程序启动失败__newline __newline____newline__描述:__newline____newline__Field abcd in xyzxxController 需要一个无法找到的 'abABC' 类型的 bean。__newline____newline__ 注入点有以下注解:newline - @org.springframework.beans.factory.annotation.Autowired(required=true)__newline____newline____newline__Action:__newline____newline__ 考虑定义在你的配置中一个 'glance.content.server.beacon.BeaconFactory' 类型的 bean。newline " , "logger": "org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter", "method": "report", "ex": ""}}

如何初始化对象或如何在这里使用 ABC 的对象?我对spring和java很陌生。

标签: javaspring-boot

解决方案


在 SpringBootApplication 类上使用 @Import(ABC.class) 或在您初始化和配置 ABC 类 (@Configuration+@Bean) 的位置创建 bean


推荐阅读