Does the <import>
command of the Spring bean container eliminate duplicate containers? For example, if bean container file A imports B and C and each these in turn import D, does Spring eliminate or ignore the duplicate D container?
It doesn't eliminate duplicate "containers", but it will eliminate duplicate bean definitions. So the beans in D will only be created once in the resulting bean factory. You'll get a face full of warnings about it, though.
It's something best avoided. One bean definition which has the same ID as another will "hide" that bean definition, regardless of whether or not the type and properties of that bean are the same. Which one gets "hidden" depends on the declaration order. It's dangerous, and so Spring will warn you about it.
I created example project spring-context-import on GitHub to confirm skaffman's answer:
$ mvn test
.
.
.
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running ca.derekmahar.example.springContextImport.SpringContextImportTest
2011-03-15 16:25:44,980 545 [main] INFO o.springframework.test.context.TestContextManager - @TestExecutionListeners is not present for class [class ca.derekmahar.example.springContextImport.SpringContextImportTest]: using defaults.
2011-03-15 16:25:45,240 805 [main] INFO o.s.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [spring-application-context.xml]
2011-03-15 16:25:45,417 982 [main] INFO o.s.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [spring-application-context-a.xml]
2011-03-15 16:25:45,459 1024 [main] INFO o.s.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [spring-application-context-b.xml]
2011-03-15 16:25:45,484 1049 [main] INFO o.s.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [spring-application-context-d.xml]
2011-03-15 16:25:45,551 1116 [main] INFO o.s.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [spring-application-context-c.xml]
2011-03-15 16:25:45,585 1150 [main] INFO o.s.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [spring-application-context-d.xml]
2011-03-15 16:25:45,610 1175 [main] INFO o.s.b.factory.support.DefaultListableBeanFactory - Overriding bean definition for bean 'd': replacing [Generic bean: class [ca.derekmahar.example.springContextImport.bean.D]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [spring-application-context-d.xml]] with [Generic bean: class [ca.derekmahar.example.springContextImport.bean.D]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [spring-application-context-d.xml]]
2011-03-15 16:25:45,652 1217 [main] INFO o.s.context.support.GenericApplicationContext - Refreshing org.springframework.context.support.GenericApplicationContext@105738: startup date [Tue Mar 15 16:25:45 EDT 2011]; root of context hierarchy
2011-03-15 16:25:45,895 1460 [main] INFO o.s.b.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18825b3: defining beans [d,b,c,a,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy
2011-03-15 16:25:45,895 1460 [main] INFO ca.derekmahar.example.springContextImport.bean.C - Created D
2011-03-15 16:25:45,979 1544 [main] INFO ca.derekmahar.example.springContextImport.bean.B - Created B
2011-03-15 16:25:45,996 1561 [main] INFO ca.derekmahar.example.springContextImport.bean.C - Created C
2011-03-15 16:25:46,005 1570 [main] INFO ca.derekmahar.example.springContextImport.bean.A - Created A
2011-03-15 16:25:46,038 1603 [main] INFO ca.derekmahar.example.springContextImport.bean.A - Running A
2011-03-15 16:25:46,038 1603 [main] INFO ca.derekmahar.example.springContextImport.bean.B - Running B
2011-03-15 16:25:46,038 1603 [main] INFO ca.derekmahar.example.springContextImport.bean.C - Running D
2011-03-15 16:25:46,038 1603 [main] INFO ca.derekmahar.example.springContextImport.bean.C - Running C
2011-03-15 16:25:46,038 1603 [main] INFO ca.derekmahar.example.springContextImport.bean.C - Running D
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.846 sec
As skaffman predicted, notice the "Overiding bean definition for bean 'd'" message which flags the duplicate bean "d".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With