Announcement Announcement Module
Collapse
No announcement yet.
ref local does not preserve local bean definitions ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ref local does not preserve local bean definitions ?

    Hi all,

    i've an questing concerning application context definitions.

    Suggest the following project setup.

    Bean1: test1.MyBean
    Bean2: test2.MyBean
    Bean3: test3.UserA
    Bean4: test3.UserB

    There are two bean factory definitions (as xml)
    context-a.xml:
    Code:
    <bean id="MyBean" class="test1.MyBean"/>
    	
    <bean id="UserA" class="test3.UserA">
    	<property name="bean">
    		<ref local="MyBean"/>
    	</property>
    </bean>
    context-b.xml
    Code:
    <bean id="MyBean" class="test2.MyBean"/>
    	
    <bean id="UserB" class="test3.UserB">
    	<property name="bean">
    		<ref local="MyBean"/>
    	</property>
    </bean>
    The ApplicationContext is loaded using the following code:
    Code:
    this.applicationContext = new ClassPathXmlApplicationContext(
    	new String[]{
    		"context-a.xml",
    		"context-b.xml"
    	}
    );
    On context initialization spring gives me the following exception:
    Code:
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserA' defined in class path resource [context-a.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyAccessExceptionsException: PropertyAccessExceptionsException (1 errors); nested propertyAccessExceptions are: [org.springframework.beans.TypeMismatchException: Failed to convert property value of type [test2.MyBean] to required type [test1.MyBean] for property 'bean']
    PropertyAccessExceptionsException (1 errors)
    org.springframework.beans.TypeMismatchException: Failed to convert property value of type [test2.MyBean] to required type [test1.MyBean] for property 'bean'
    Enabling the debug logging is could see:
    Code:
    INFO  org.springframework.beans.factory.support.DefaultListableBeanFactory - Overriding bean definition for bean 'MyBean': replacing [Root bean: class [test1.MyBean]; abstract=false; singleton=true; lazyInit=false; autowire=0; dependencyCheck=0; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [context-a.xml]] with [Root bean: class [test2.MyBean]; abstract=false; singleton=true; lazyInit=false; autowire=0; dependencyCheck=0; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [context-b.xml]]
    My question is, why the 'ref local' definition does not use the local bean definition per context.
    Is there a way (beside just renaming the booth MyBean-definitions to unique names) to force the BeanFactory to use the local bean references ?

    Greets and Thanx for your suggestions
    Sebastian Wiemer
    Last edited by snwiem; Jun 21st, 2006, 04:04 AM.

  • #2
    I also encountered this behavior before. The point is, that ref-local just ensures on xml level that the referenced entity exists, while a normal ref could fail only on a real access at runtime.
    When overriding a referenced entity it is still existing, so the contract of ref-local still holds. ref-local was (afaik) never intended to prevent overriding.

    As for preventing overriding in general, see the "allowBeanDefinitionOverriding" property of DefaultListableBeanFactory.

    Regards,
    Andreas

    Comment


    • #3
      The 'allowBeanDefinitionOverriding' on DefaultListableBeanFactory works
      as expected.

      Unforunately i'm using spring witin a web container loading the bean definition files using the ContextLoaderListener.

      I couldn't find a solution to set the allowBeanDefinitionOverwriting having a look in the documentation, source code and dtd definition.

      So is it true, that i have to implement my own XmlWebApplicationContext class and configure it withing the ContextLoader.properties file in spring.jar ?!?


      Would be nice to see any other solutions.

      greets
      Sebastian Wiemer

      Comment


      • #4
        At least I do not know about an alternative solution to the one you already came up with (using a subclass).

        Regards,
        Andreas

        Comment

        Working...
        X