![](/img/trans.png)
[英]Spring xml initializes beans when declared in child class but if the same is declared in parent class bean initialization fails
[英]Spring Bean entry with Same Alias - one Child another Parent class
我需要在SPRING bean中使用相同的Alias覆蓋默認的服務類,以便使用相同的Alias調用新的服務類。 我在Spring xml中添加了以下代碼,此處“ CustomDefaultService擴展了DefaultService”
1)Spring在創建實例和通過相同的別名引用時是否優先於子類?
2)或者如果我們有孩子或父母分配給相同的別名,它是Spring的隨機變量?
<alias alias="modelService" name="customMefaultService" />
<bean id="customMefaultService" class="com.tisl.CustomDefaultService" parent="defaultModelService">
</bean>
實際使用哪個bean取決於上下文加載順序。 Spring將使用創建的最后一個bean 。 因此,如果可以確保在DefaultService
之后創建customMefaultService
。
為了確保順序,您可以使用depends-on
。 例如:
<alias alias="modelService" name="customMefaultService" />
<bean id="customMefaultService" class="com.tisl.CustomDefaultService" parent="defaultModelService" depends-on="defaultModelService">
</bean>
由於您已經在使用parent
我認為順序已經正確。
還要確保將setAllowBeanDefinitionOverriding
設置為true
(默認為true
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.