开发者

Spring bean name when importing another spring context

开发者 https://www.devze.com 2023-01-18 11:22 出处:网络
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

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">

   <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">

   <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">

   <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.g开发者_如何学编程etClass().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


From Spring's documentation:

Every bean has one or more ids (also called identifiers, or names; these terms refer to the same thing). These ids must be unique within the BeanFactory or ApplicationContext the bean is hosted in.

According to this, your combined application context is invalid as it contains two different beans which have the same ID - your bean named "name2" from ContextA.xml and your bean named "name2", aliased "name3" in ContextC.xml. I would expect Spring to issue at least a warning about this.

To answer your question: You shouldn't expect any sane results from this kind of setup. Bean names have to be unique and if they aren't the results are undefined. And by "undefined" I mean "unlikely to be helpful" :)

Hope this helps.


I believe you are seeing different results because Spring is loading the beans in the context in different orders in each scenario. Spring makes no guarantee as to which order it will load it's beans other than the fact that any beans used as "ref"'s in other bean definitions will be loaded before the beans that depend on them.

The correct solution to your problem is DO NOT use duplicate bean identifiers and then you won't have to guess as to which bean you will get when you look one up.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消