gpt4 book ai didi

java - Spring 3+如何 Autowiring 相互使用的bean?

转载 作者:搜寻专家 更新时间:2023-10-31 19:56:37 25 4
gpt4 key购买 nike

比如我有

@Service
public class UserSerice {
@Autowired
private HouseService houseService;
}

@Service
public class HouseService {
@Autowired
private UserSerice userService;
}

Spring 将如何 Autowiring 它?以这种方式配置 bean 是一种好的做法吗?

最佳答案

Circular dependencies (spring-framework-reference):

For example: Class A requires an instance of class B through constructor injection, and class B requires an instance of class A through constructor injection...throws a BeanCurrentlyInCreationException.

it is not recommended... One possible solution is to edit the source code of some classes to be configured by setters rather than constructors...

加上:

我以 setter 方式调试了循环依赖。 序列似乎是:

-> 开始创建bean A

-> 开始创建bean B

-> 将A注入(inject)B,虽然A从Spring生命周期的角度来看并没有完全创建

->Bean B 创建完成

-> 将 bean B 注入(inject) A

-> Bean A 创建

关于java - Spring 3+如何 Autowiring 相互使用的bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13739056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com