2016-11-11 27 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> 
相关问题