This is from Spring documentation, section 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>
Now, the note below the code says:
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.
Could someone please explain how this works. I know the various scopes. So I know that a new instance needs to be returned if advisors are prototype. But I did not get the last statement (highlighted in bold). If I specify ref="debugInterceptor" will the bean creation mechanism change? Does it mean that if I ref a bean, it will be a singleton instance.
No, but the bean being referred to will be instantiated when the referring bean is instantiated. If Bean A (singleton scope) has a
refto Bean B (prototype scope), then Bean B will be created and injected into Bean A just the once. As far as Bean A is concerned, Bean B is a singleton.The
ProxyFactoryBeanmust hold a list of bean names so that it doesn’t force the dereferencing of the interceptors when theProxyFactoryBeanitself is created. By holding a list of names, it can defer the lookup of the interceptors until the last minute, allowing the interceptors to be prototype scoped beans.Does that help?