Does the Spring bean container command eliminate duplicate containers?

前端 未结 2 472
醉酒成梦
醉酒成梦 2021-01-17 14:13

Does the command of the Spring bean container eliminate duplicate containers? For example, if bean container file A imports B and C and each the

相关标签:
2条回答
  • 2021-01-17 14:59

    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.

    0 讨论(0)
  • 2021-01-17 15:04

    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".

    0 讨论(0)
提交回复
热议问题