Results 1 to 5 of 5

Thread: Bean redefinition in the context xml hierarchy

  1. #1
    Join Date
    Nov 2008
    Posts
    3

    Default Bean redefinition in the context xml hierarchy

    Hi All,

    I found the following behavior of Spring framework but I didn't manage to find corresponding official documentation explaining it.
    Let's say we have 2 file A.xml & B.xml. A.xml imports B.xml and both of them contain beans with the same name, i.e:

    A.xml:
    Code:
    <import resource="classpath:B.xml"/>
    <bean id="testBeanName" class="TestClassA"/>
    B.xml:
    Code:
    <bean id="testBeanName" class="TestClassB"/>
    When I load the configuration and tell ApplicationContext get me the bean with name "testBeanName" it will return me one from A.xml file. It's kind of shadowing. Is it a side effect of some bean name resolving mechanism or it's a feature provided by Spring? Are there any guaranties that ApplicationContext will always return me the bean defined in the 'top-level' xml file? And where can I read about that?
    Thank you in advance.

  2. #2
    Join Date
    Nov 2008
    Posts
    3

    Default

    hm, now ideas?

  3. #3
    Join Date
    Sep 2008
    Location
    London, UK
    Posts
    155

    Default

    The import statement is declared before TestBeanA, so its very logical that spring defines TestBeanB first and redefine it to TestBeanA second.

    Just swap the TestBeanA definition and the import statement and you will see the opposite.

    Cheers

    G

  4. #4
    Join Date
    Nov 2008
    Posts
    3

    Default

    Hi Goran,

    thanks for answering. I know the behavior. The problem is that I'm designing some sophisticated context dependencies between several xml files and one of the points which I rely on is this bean redefinition. The project architect who never heard about this 'feature' asked me to provide corresponding documentation where it is clearly explained. But so far I have not managed to find one. Could you or anyone point me to the specification/javadoc/anything which I can present as an evidence of my rightness?

  5. #5
    Join Date
    Sep 2008
    Location
    London, UK
    Posts
    155

    Default

    I don't know your architect, but honestly, he/she should be happy if you put together a couple of lines about your discoveries yourself.

    You might want to read 3.6. Bean definition inheritance in the documentation, since this might be relevant to what you are doing.

    Cheers

    G

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •