【发布时间】:2025-12-19 22:20:15
【问题描述】:
在SPRING BOOT application 我有一个类如下
@Service
public class XYZ{
}
我想在其他类ABC中使用上面的
public class ABC{
@Autowired
private XYZ xyx;
}
它会抛出无法找到 XYZ 的错误。我已经在编写 main 方法的类中有@SpringBootApplication。因此,这将自动在包上启用@ComponentScan。我的理解是,由于 XYZ 已使用@service 进行注释,因此 spring 扫描并创建和注册该 bean。如何在不使用 xml 配置的情况下访问其他类中的 bean?
【问题讨论】:
-
在询问错误时,请发布实际的相关代码,以及错误的完整且准确的堆栈跟踪。我的猜测是该错误与不尊重此处描述的约定有关:docs.spring.io/spring-boot/docs/current/reference/htmlsingle/…
标签: java spring spring-boot spring-bean