【问题标题】:error - a bean of type 'package' while injecting dependency of one service into another services controller[Spring-Boot]错误 - 将一个服务的依赖项注入另一个服务控制器时,一个“包”类型的 bean [Spring-Boot]
【发布时间】:2021-07-21 15:18:26
【问题描述】:

我有一个 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", “级别”:“错误”,“日志”: "换行____换行换行__应用程序失败 START__newline__newline____newline__Description:__newline____newline__x.y.z.xxController 中的字段 abcd 需要一个 bean 输入 'a.b.ABC' 那不可能 发现.__newline____newline__注入点有以下 注释:换行 - @org.springframework.beans.factory.annotation.Autowired(required=true)__newline____newline____newline__Action:__newline____newline__考虑定义一个'glance.content.server.beacon.BeaconFactory'类型的bean 在您的配置中。换行符", "logger": "org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter", “方法”:“报告”,“前”:“”}}

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

【问题讨论】:

    标签: java spring-boot


    【解决方案1】:

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

    【讨论】:

    • 目前 ABC 的代码是这样的 @Service public class ABC {
    • 您是说要添加这两个注释。是吗?
    • @Import(ABC.class) 或 (@Configuration+@Bean)
    猜你喜欢
    • 2022-12-20
    • 1970-01-01
    • 2013-12-22
    • 2020-01-27
    • 2020-09-04
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多