java - 错误 - 将一个服务的依赖项注入另一个服务控制器时,一个“包”类型的 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很陌生。
解决方案
在 SpringBootApplication 类上使用 @Import(ABC.class) 或在您初始化和配置 ABC 类 (@Configuration+@Bean) 的位置创建 bean
推荐阅读
- c++ - 变量名可以与 C++ 中的函数名相同吗?
- java - 使用 Spring Boot Data JPA 按需创建表
- c - 将字符串数组作为参数传递给函数
- php - 如何获取 php lararavel 中的总运行余额列?
- javascript - 类中的 jQuery 脚本问题
- couchdb - OpenWhisk“动作调用”返回带有签名的问题
- c# - 正则表达式匹配文本不包含给定列表中的任何(多个)单词
- python - Python 和 Selenium:无法找到要单击的特定 (href) 元素
- java - 使用 Jackson/FasterXML 读取 XML 时如何忽略某些元素?
- c++ - 我可以在 C++ 中返回一个跨度吗?如果是这样,怎么做?(对替代品开放。)