覆盖没有别名(也是父对象)的spring bean [英] Override spring bean without an alias that is also the parent

查看:108
本文介绍了覆盖没有别名(也是父对象)的spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 module1-spring.xml -

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

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

我想覆盖 module2-spring.xml -

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

我希望在服务中传递child,而不是parent. 如果我将child别名为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"/>

然后,parent属性将读取子级而不是父级Bean,并且在服务器启动时失败,并显示错误-

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

BeanDefinitionStoreException:具有名称的无效bean定义 在类路径资源[module2-spring.xml]中定义的子级":无法 解决父bean定义父".

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?

推荐答案

module2 中复制 service bean并注入 child 解决了该问题. .
module2-spring.xml-

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>

这篇关于覆盖没有别名(也是父对象)的spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆