Spring bean container的<import>
命令是否会消除重复的容器?例如,如果bean容器文件A导入B和C并且每个导入D又导入D,Spring是否会消除或忽略重复的D容器?
答案 0 :(得分:12)
它不会消除重复的“容器”,但它会消除重复的bean定义。因此,D中的bean只会在生成的bean工厂中创建一次。不过,你会得到一张充满警告的脸。
这是最好的避免。一个与另一个具有相同ID的bean定义将“隐藏”该bean定义,无论该bean的类型和属性是否相同。哪一个“隐藏”取决于声明顺序。这很危险,所以Spring会警告你。
答案 1 :(得分:3)
我在spring-context-import上创建了示例项目GitHub,以确认skaffman的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
正如skaffman预测的那样,请注意“bean'的'overiding bean definition'''消息,它标记了重复的bean”d“。