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:
context-b.xmlCode:<bean id="MyBean" class="test1.MyBean"/> <bean id="UserA" class="test3.UserA"> <property name="bean"> <ref local="MyBean"/> </property> </bean>
The ApplicationContext is loaded using the following code:Code:<bean id="MyBean" class="test2.MyBean"/> <bean id="UserB" class="test3.UserB"> <property name="bean"> <ref local="MyBean"/> </property> </bean>
On context initialization spring gives me the following exception:Code:this.applicationContext = new ClassPathXmlApplicationContext( new String[]{ "context-a.xml", "context-b.xml" } );
Enabling the debug logging is could see: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'
My question is, why the 'ref local' definition does not use the local bean definition per context.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]]
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


Reply With Quote