Announcement Announcement Module
Collapse
No announcement yet.
multiple configs, overriding bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • multiple configs, overriding bean

    It is possible to use more than one config file, but I haven't figured out what the logic is for handling beans with the same ID. Does it pick the first bean identified by the ID, the last one, or one at random?

    Here is an example. I have a bean with an ID of "dataSource in hibernate.xml. I would like to override it with the "dataSource" bean in dataSource.xml. How would I do that? Should either of these work?

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    <beans>
      <bean id="configs" class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg>
          <list>
            <value>dataSource.xml</value>
            <value>hibernate.xml</value>
          </list>
        </constructor-arg>
      </bean>
    </beans>
    or

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    <beans>
      <bean id="dataSourceConfig" class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg>
          <list>
            <value>dataSource.xml</value>
          </list>
        </constructor-arg>
      </bean>
      
      <bean id="configs" class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg>
          <list>
            <value>hibernate.xml</value>
          </list>
        </constructor-arg>
        <constructor-arg>
          <ref local="dataSourceConfig"/>
        </constructor-arg>
      </bean>
    </beans>
    I'm using Spring v1.1.3. Any ideas?

    Cameron

  • #2
    ref local picks a bean from the same file or else fails. I'm not sure how ref bean behaves if multiple beans with the same id are present.

    Comment


    • #3
      Originally posted by tentacle
      ref local picks a bean from the same file or else fails. I'm not sure how ref bean behaves if multiple beans with the same id are present.
      That is what I'm asking. How does <ref bean="dataSource"/> work when I have a couple different config files each with a definition of "dataSource"? Does one override the other and how I determine which one I'll get? I tried a few combinations without figuring out the pattern.

      Cameron

      Comment


      • #4
        Originally posted by ctaggart
        How does <ref bean="dataSource"/> work when I have a couple different config files each with a definition of "dataSource"? Does one override the other and how I determine which one I'll get? I tried a few combinations without figuring out the pattern.
        I experienced that later definitions override earlier ones. I used that exactly in combination with multiple configuration files to override some definitions as needed.

        A further indication is the existence of this method in DefaultListableBeanFactory:

        Code:
        setAllowBeanDefinitionOverriding
        
        public void setAllowBeanDefinitionOverriding&#40;boolean allowBeanDefinitionOverriding&#41;
        
            Set if it should be allowed to override bean definitions by registering a different definition with the same name, automatically replacing the former. If not, an exception will be thrown. Default is true.

        Regards,
        Andreas

        Comment


        • #5
          In the created ApplicationContext there can only be one bean stored under an id.
          So, if there is more than one bean definition with the same id,
          the behaviour depends on the isAllowBeanDefitionOverriding-Property of DefaultListableBeanFactory, which defaults to true (unfortunately in my opinion).
          With this default, the last bean definition with the same id wins, respectively to the order of the xml files.
          In one xml file there can only be one bean definition with the same id due to the dtd.

          If you have control over the creation of the application context you can use something like this:


          ApplicationContext vContext = new ClassPathXmlApplicationContext(aSpringFileNames) {
          protected DefaultListableBeanFactory createBeanFactory() {
          final DefaultListableBeanFactory vResult = super.createBeanFactory();
          vResult.setAllowBeanDefinitionOverriding(false);
          return vResult;
          }
          };

          Regards,
          Rurik

          Comment


          • #6
            Originally posted by epsilontik
            In one xml file there can only be one bean definition with the same id due to the dtd.
            That is true. However, instead of an id you can use a name. In that case multiple beans with the same name can exist in one file.
            Here again, the last definition wins.

            Regards,
            Andreas

            Comment

            Working...
            X