Spring bean 容器是否?命令消除重复的容器?

发布于 2024-10-21 15:08:37 字数 386 浏览 4 评论 0 原文

Spring bean容器消除重复容器?例如,如果bean容器文件A导入B和C,并且每个文件依次导入D,Spring是否消除或忽略重复的D容器?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

篱下浅笙歌 2024-10-28 15:08:37

它不会消除重复的“容器”,但会消除重复的 bean 定义。因此,D 中的 bean 只会在生成的 bean 工厂中创建一次。不过,你会看到一张充满警告的脸。

这是最好避免的事情。与另一个具有相同 ID 的 Bean 定义将“隐藏”该 Bean 定义,无论该 Bean 的类型和属性是否相同。哪一个被“隐藏”取决于声明顺序。这是危险的,所以 Spring 会警告你。

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.

小伙你站住 2024-10-28 15:08:37

我在 spring-context-import /github.com" rel="nofollow noreferrer">GitHub 确认 skaffman 的 答案

$ 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 预测的那样,请注意“Overiding beanDefinition for bean 'd'”消息,该消息标记了重复的 bean“d”。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文