Announcement Announcement Module
Collapse
No announcement yet.
Bean redefinition in the context xml hierarchy Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    hm, now ideas?

    Comment


    • #3
      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

      Comment


      • #4
        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?

        Comment


        • #5
          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

          Comment

          Working...
          X