Announcement Announcement Module
Collapse
No announcement yet.
two beans with same name is permited in ApplicationContex Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • two beans with same name is permited in ApplicationContex

    Hello Friend:

    It is murky to me asking to Spring Team about the beans imported in a ApplicationContext with same name or id .

    I have one ApplicationContext.xml that import a other ApplicationContext file called ApplicationContextB.xml

    Code:
    <import resource="ApplicationContextB.xml">
    <bean id="classA" class="ClassA" destroy-method="close">
        <property name="aString" value="ClassAFirst"/>
     </bean />
    In the file ApplicationContextB.xml I have put
    Code:
    <bean id="classA" class="ClassA" destroy-method="close">
        <property name="aString" value="ClassALast"/>
     </bean />
    Now, when execute my Application Sample, that work fine (it executed without any exceptions even its does repeated the name.).

    But, when I get a bean with name "ClassA", Spring got me a last bean rather than a first bean.

    How Can I know that? After got a bean, I print the content of the beanīs property "aString", that show the message "ClassALast".

    Itīs stranger true?

    It seens that Spring always the last bean founded (in case of they use the same ID or name) when import other beans from other applicationContext.xml

    Spring do it for any reason? or It... , in worst case, is a Bug?

    Thanks in advance.

  • #2
    This is a feature. In fact it comes in handy sometimes. For example, you can "override" a normal bean with a bean that is specific for unit testing.

    Hmmm. I went to the reference guide to make sure I'm correct, but I don't see any mention of this. In the Javadocs there is this:

    Code:
    Inheritance from a parent context. Definitions in a descendant context will always take priority. This means, for example, that a single parent context can be used by an entire web application, while each servlet has its own child context that is independent of that of any other servlet.
    --- Josef

    Comment


    • #3
      I think

      Hello Friend:

      Thanks for you reply. In the post you say "It is a feature"... I want to know: why this feature? that difficults a lot to applications Integration... I have this problem now. I had to change theirs repeated beans name one by one....

      So... i guess that was a "feature"? and.. that feature is designed for???

      Comment


      • #4
        it might be a feature or a bug.
        But! such a behaviour is expected in many cases, so bean overriding is a feature indeed.
        However, if you have many beans with the same name in your app, I'd say it's not designed in the best way. I wouldn't be surprised, if you say, that you have lots of classes with the same names

        Comment


        • #5
          Thank you

          Originally posted by Injecteer
          it might be a feature or a bug.
          But! such a behaviour is expected in many cases, so bean overriding is a feature indeed.
          However, if you have many beans with the same name in your app, I'd say it's not designed in the best way. I wouldn't be surprised, if you say, that you have lots of classes with the same names
          In effect, our developing team is composited in two lines:
          Developing Line and Production Line.

          I belong to Developing Line and exist other team that belongs to Production Line.

          So we are developing with our ApplicationContext and they, of course, their ApplicationContext.

          When we went to integrate our applicationContext, the problem of repeated beanīs name is ocurried.

          I think, that can be useful if add a new Attribute to the tag Bean like "context" , itīs funtionality is specifie in which context some bean is belong.

          It is only my idea, and I believe Spring Team can do something major.

          Thanks

          Comment


          • #6
            there are many way to solve or workaround the issue. The simpliest one is to use "local" instead of "id" attr to reference the beans in the ctx files.
            look here http://static.springframework.org/sp...ns-ref-element.
            you can also, for example, play with aliases or add some suffixes to the bean names, like "dao:logger" and "remote:logger".
            you can have a look at the custom XML-Schema beanDefs...

            Comment


            • #7
              Thanks

              Thanks Friend:

              I had did it. That worked fine when Spring is injecting beans into other beans. But, in case when my code get a "bean" from the object ApplicationContext, like this:

              Code:
                   ClassA class = ctx.getBean("classA");
              That will get the last ClassA bean rather than the first.

              So, still know not to know this feature is a Bug or a Advantage.

              Comment


              • #8
                >>So, still know not to know this feature is a Bug or a Advantage.
                well, if you use it properly, it's a feature. If you misuse it it's a bug

                Comment

                Working...
                X