【发布时间】:2018-11-04 18:45:47
【问题描述】:
我有一个模型 A 和 B 以及一个关系模型 A_Bp>
现在我想让 A 类中的 getter 从 A-B 类中获取一些东西 所以我在模型 A 中 @Autowired 了 A_B 的 repo,但它给出了一个错误。
有没有办法使用类似 Autowire 的 postconstruct?
这是我的代码:
@autowire IrepoA_B irepoA_B;
@PostConstruct
public boolean getVar() {
return irepoA_B.getByTrackId(this.getId()).var();
}
我该如何解决这个问题,以便他不会给出错误:
创建类中定义的名称为“entityManagerFactory”的bean时出错
路径资源…………
【问题讨论】:
-
为什么
@PostConstruct在吸气剂上?它应该是一个void方法。这可能(并且很可能)会导致过早查询数据库(在所有设置完成之前)并给您错误)。 -
我希望它不会初始化自动装配,直到它被使用,所以根据我的(有缺陷的)逻辑它只会在启动应用程序后初始化 repo
-
No... 只要创建了对象并且满足了依赖关系,就会调用
@PostConstruct。这甚至可能在创建事务代理等之前。
标签: spring-boot autowired postconstruct