gpt4 book ai didi

java - Spring 在 ProxyFactoryBean 中使用 ref 引用原型(prototype) bean

转载 作者:行者123 更新时间:2023-12-01 05:37:08 24 4
gpt4 key购买 nike

这是来自 Spring documentation ,第 9.5.4 节

<bean id="myAdvisor" class="com.mycompany.MyAdvisor">
<property name="someProperty" value="Custom string property value"/>
</bean>

<bean id="debugInterceptor"class="org.springframework.aop.interceptor.DebugInterceptor">
</bean>

<bean id="person"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.mycompany.Person"/>

<property name="target" ref="personTarget"/>
<property name="interceptorNames">
<list>
<value>myAdvisor</value>
<value>debugInterceptor</value>
</list>
</property>
</bean>

现在,代码下面的注释显示:

You might be wondering why the list doesn't hold bean references. The reason for this is that if the ProxyFactoryBean's singleton property is set to false, it must be able to return independent proxy instances. If any of the advisors is itself a prototype, an independent instance would need to be returned, so it's necessary to be able to obtain an instance of the prototype from the factory; holding a reference isn't sufficient.

有人可以解释一下这是如何工作的吗?我知道各种范围。所以我知道如果顾问是原型(prototype),则需要返回一个新实例。但我没有得到最后一句话(以粗体突出显示)。如果我指定 ref="debugInterceptor" bean 创建机制会改变吗?这是否意味着如果我ref一个bean,它将是一个单例实例。

最佳答案

Does it mean that if I ref a bean, it will be a singleton instance.

否,但是当引用的 bean 被实例化时,被引用的 bean 也会被实例化。如果 Bean A(单例范围)有 Bean B(原型(prototype)范围)的ref,那么 Bean B 将被创建并注入(inject)到 Bean A 中一次。就 Bean A 而言,Bean B 是一个单例。

ProxyFactoryBean 必须保存一个 Bean 名称列表,以便在创建 ProxyFactoryBean 本身时不会强制取消对拦截器的引用。通过保存名称列表,它可以将拦截器的查找推迟到最后一刻,从而允许拦截器成为原型(prototype)作用域的 bean。

这有帮助吗?

关于java - Spring 在 ProxyFactoryBean 中使用 ref 引用原型(prototype) bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7991855/

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