tags:

views:

119

answers:

1

Can you enlighten me on this problem I encountered while experimenting with Spring.

I have 2 context here. let's name them springA.xml and springB.xml

springA.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd"&gt;

   <import resource="springB.xml" />

   <bean name="name2" class="java.lang.String"/>
</beans>

springB.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd"&gt;

   <bean name="name2,name3" class="java.lang.String"/>

</beans>

springC.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd"&gt;

   <bean name="name3,name2" class="java.lang.String"/>

</beans>

And this is my Java File.

public static void main(String[] args) {
    BeanFactory factory = new XmlBeanFactory(new ClassPathResource("springA.xml"));

    Object obj1 = factory.getBean("name2");
    Object obj2 = factory.getBean("name3");

    System.out.println(obj1.getClass().getName() + " " + obj2.getClass().getName());
}

And the result, I get a "java.lang.String java.lang.String". If I change the position of the name "name2,name3" to "name3,name2" (springC.xml), I get a "java.lang.Object java.lang.Object".

I am just confused as to why the result is like that. I was expecting that the function will return java.lang.String for name2 and java.lang.Object for name3 (since name2 is already used in the springA.xml, I am assuming this name will not be used and instead, will use name3 for springB.xml)

Thanks!

PS: Spring 2.5 Eclipse 3.5

A: 

I've ran your code on Spring 2.5.6 and 3.0.0.M1 and both version produce the same result.

java.lang.String java.lang.String

My advice is if you want two strings and you are getting strange results with 2.5, then bump to 2.5.6 or 3.0.0.M1.

dom farr
is this a bug in Spring?
qaxi
Even if I tie down the spring version to 2.5, I don't get your results. I always get String String as an output with either context. So I would so no, this isn't a spring bug.
dom farr
@qaxi I checked this with Spring 2.5.6, 2.5.5, 2.5.3, 2.0.7 and they all returned java.lang.String java.lang.String. I think something is wrong with your configuration.
Gary Rowe