2016-11-11 35 views
0

我有一個java項目,使用Spring。我定義了三個類:BaseClass,SubClass1和SubClass2。兩個SubClassx都擴展了BaseClass。 現在,我需要定義兩個bean,一個是在特定的配置文件:在春天,如何定義兩個豆,具有相同的名稱和不同的類

<bean id="newBean" class="SubClass1"> 

<beans profile="xxx"> 
    <bean id="newBean" class="SubClass2"> 
</beans> 

它們在不同的XML文件。我用龍目島注入SubClass2 newBean在A類:

public class WarmUpAgent extends WarmUpAgent { 
    @Setter(onMethod = @__(@Required)) 
    private SubClass2 subClass2; 
... 

<bean id="warmUpAgent" class="WarmUpAgent"> 
    <property name="subClass2" ref="newBean" /> 
</bean> 

但是,當我運行配置文件「XXX」的項目,它拋出:

IllegalStateException: Cannot convert value of type [SubClass1] to required type [SubClass2] for property 'subClass2' 

似乎無法覆蓋豆newBean與SubClass2 ,即使我已激活配置文件「xxx」。有沒有什麼方法可以使用配置文件來定義兩個具有相同名稱和不同類的bean?

非常感謝。

============================================== ==========================

我把

<bean id="newBean" class="SubClass1"> 

<beans profile="xxx"> 
    <bean id="newBean" class="SubClass2"> 
</beans> 

在一個XML文件,它的工作原理。根據項目組織的說法,我想將它們分成兩個xml文件。任何解決方案來實現這個?

+0

你一直在說相同的名稱,但使用的是同一個ID沒有名字! –

+0

對不起,我的錯。我也嘗試使用名稱,它不工作。 – GameBoy

+0

你能不能顯示你的2個xml文件 – kuhajeyan

回答

0

嘗試使用同名而不是id。

<bean name="newBean" class="SubClass1"> 
<beans profile="xxx"> 
    <bean name="newBean" class="SubClass2"> 
</beans> 
+0

我試過了,沒有工作。 – GameBoy

0

嘗試限制第一個bean到默認配置文件

<beans profile="default"> 
    <bean id="newBean" class="SubClass1"> 
</beans> 
<beans profile="xxx"> 
    <bean id="newBean" class="SubClass2"> 
</beans> 
相關問題