简体   繁体   中英

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 . I added the below code in Spring xml, here "CustomDefaultService extends DefaultService"

1) Does Spring give precedence to the child class in creating instance and referring via same alias name ?

2) Or its random for Spring if we have Child or parent to assign to same alias name?

<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. Spring will use the last bean created . So if you can ensure that the customMefaultService is created after the DefaultService .

To ensure the order you could use the 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.

Also make sure that setAllowBeanDefinitionOverriding is set to true (should be as true is the default)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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