简体   繁体   English

具有相同别名的Spring Bean条目-一个孩子另一个父类

[英]Spring Bean entry with Same Alias - one Child another Parent class

I have a requirement to Override a default service class with same Alias in SPRING bean.So that new service class is called with same Alias . 我需要在SPRING bean中使用相同的Alias覆盖默认的服务类,以便使用相同的Alias调用新的服务类。 I added the below code in Spring xml, here "CustomDefaultService extends DefaultService" 我在Spring xml中添加了以下代码,此处“ CustomDefaultService扩展了DefaultService”

1) Does Spring give precedence to the child class in creating instance and referring via same alias name ? 1)Spring在创建实例和通过相同的别名引用时是否优先于子类?

2) Or its random for Spring if we have Child or parent to assign to same alias name? 2)或者如果我们有孩子或父母分配给相同的别名,它是Spring的随机变量?

<alias alias="modelService" name="customMefaultService" />
    <bean id="customMefaultService" class="com.tisl.CustomDefaultService" parent="defaultModelService">
</bean>

Which bean is actually used depends on the context loading order. 实际使用哪个bean取决于上下文加载顺序。 Spring will use the last bean created . Spring将使用创建的最后一个bean So if you can ensure that the customMefaultService is created after the DefaultService . 因此,如果可以确保在DefaultService之后创建customMefaultService

To ensure the order you could use the depends-on . 为了确保顺序,您可以使用depends-on Eg: 例如:

<alias alias="modelService" name="customMefaultService" />
    <bean id="customMefaultService" class="com.tisl.CustomDefaultService" parent="defaultModelService" depends-on="defaultModelService">
</bean>

As you are already using parent I would assume that the order is already correct anyway. 由于您已经在使用parent我认为顺序已经正确。

Also make sure that setAllowBeanDefinitionOverriding is set to true (should be as true is the default) 还要确保将setAllowBeanDefinitionOverriding设置为true (默认为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM