Override spring bean without an alias that is also the parent

别来无恙 提交于 2019-12-20 05:40:35

问题


I have a bean in module1-spring.xml-

<bean id="parent" class="com.Parent"/>

<bean id="service" class="com.Service">
    <property name="parent" ref="parent"/>
</bean>

I want to override the bean in module2-spring.xml-

<bean id="child" class="com.Child" parent="parent"/>

I want child to be passed in service instead of parent. If I alias child as parent i.e.

<alias id="child" alias="parent"/>

then the parent attribute will read child instead of parent bean and fail on server startup with error-

BeanDefinitionStoreException: Invalid bean definition with name 'child' defined in class path resource [module2-spring.xml]: Could not resolve parent bean definition 'parent'.

What is the correct way of overriding parent while its also the parent of child?


回答1:


Copying the service bean in module2 and injecting child fixed the issue.
module2-spring.xml-

<bean id="service" class="com.Service">
    <property name="parent" ref="child"/>
</bean>


来源:https://stackoverflow.com/questions/53606472/override-spring-bean-without-an-alias-that-is-also-the-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!