Prototype reference inside a Singleton
在Spring容器中,如果定义了一个Singleton作用域的Bean,但是属性Bean是一个prototype作用域的,会怎么样?
如果按照平常类似<bean></bean>的格式注入,是不可以。唯一的办法是将prototype这个Bean对象改成ProxyBean。
<bean name="bookServiceBean" id="bookServiceBean"
class="spring.test.xmlBeanFactory.bean.BookServiceBean" scope="prototype"></bean>
<bean name="penServiceBean" class="spring.test.xmlBeanFactory.bean.PenServiceBean"></bean>
<bean name="toolServiceBean" class="spring.test.xmlBeanFactory.bean.StudyToolServiceBean">
<property name="bookServiceBean" ref="proxyBean" />
</bean>
<bean id="prototypeTargetSource" class="org.springframework.aop.target.PrototypeTargetSource">
<property name="targetBeanName" value="bookServiceBean" />
</bean>
<bean id="proxyBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetSource" ref="prototypeTargetSource" />
</bean>
Spring还有一种MethodInject的方式 具体可以看:http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-factory-method-injection