Does the Spring bean container <import> command eliminate duplicate containers? Does the Spring bean container <import> command eliminate duplicate containers? spring spring

Does the Spring bean container <import> command eliminate duplicate containers?


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.SpringContextImportTest2011-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 hierarchy2011-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 hierarchy2011-03-15 16:25:45,895 1460 [main] INFO  ca.derekmahar.example.springContextImport.bean.C - Created D2011-03-15 16:25:45,979 1544 [main] INFO  ca.derekmahar.example.springContextImport.bean.B - Created B2011-03-15 16:25:45,996 1561 [main] INFO  ca.derekmahar.example.springContextImport.bean.C - Created C2011-03-15 16:25:46,005 1570 [main] INFO  ca.derekmahar.example.springContextImport.bean.A - Created A2011-03-15 16:25:46,038 1603 [main] INFO  ca.derekmahar.example.springContextImport.bean.A - Running A2011-03-15 16:25:46,038 1603 [main] INFO  ca.derekmahar.example.springContextImport.bean.B - Running B2011-03-15 16:25:46,038 1603 [main] INFO  ca.derekmahar.example.springContextImport.bean.C - Running D2011-03-15 16:25:46,038 1603 [main] INFO  ca.derekmahar.example.springContextImport.bean.C - Running C2011-03-15 16:25:46,038 1603 [main] INFO  ca.derekmahar.example.springContextImport.bean.C - Running DTests 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".