gpt4 book ai didi

java - Spring 的不同范围

转载 作者:行者123 更新时间:2023-12-02 01:53:06 26 4
gpt4 key购买 nike

如果我们创建两个类的bean,一个具有单例范围,另一个具有原型(prototype)范围。如果singleton bean引用prototype bean,将会创建多少个实例?内部会发生什么?

最佳答案

Spring 是作为 IoC(控制反转)框架诞生的。来自 documentation :

IoC is also known as dependency injection (DI). It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself controlling the instantiation or location of its dependencies by using a direct construction of classes, or a mechanism such as the Service Locator pattern.

在您的情况下:如果 A 是引用 B 对象范围原型(prototype)的单例对象,则 B 将被实例化一次,因为 A 只会调用它一次(在其创建期间)。

关于java - Spring 的不同范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52697185/

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