【发布时间】:2020-11-24 17:04:55
【问题描述】:
我有如下服务
@Service
public class A {
public B data() {
InterfaceA B = (codition) ? new B1() : new B2();
B.check();
}
}
@Service
public class B1 {
@Autowired
private B1Repo b1repo;//repository
public B1 check() {
b1repo.find();
}
}
当我运行服务A.data() 进程时,我将b1repo 设为空。由于我使用的是 new 关键字,因此存储库不是自动装配的。
我已经检查了以下问题
related_issue
但它并没有帮助我解决问题
【问题讨论】:
-
您将其设为 null 是因为您正在创建自己的对象(使用 new),而不是使用使用 @Service 类创建的 DI 对象。您必须在 A 类中为 B1 使用 Autowire。
-
你能根据你的评论更新上面的代码吗?我不知道如何在 DI 中注入服务
标签: java spring-boot autowired