當幾個Spring bean被定義爲相同的名稱時,哪一個會隱藏其他bean?Spring bean定義的優先級是什麼?
比方說,我已經標註了在包org.example @Component("bean")
幾類,以及包含一個applicationContext.xml中:當我做一個applicationContext.getBean("bean")
<context:component-scan base-package="org.example"/>
<alias name="aliasedBean" alias="bean"/>
<bean id="aliasedBean" class="org.example.AliasedBean"/>
<bean id="bean" class="org.example.XmlBean"/>
<import resource="otherApplicationContext.xml"/>
至極bean將被檢索?
每個bean都有一個或多個標識符。這些標識符在託管bean的容器內必須是唯一的。
但是,我知道(因爲我測試過),Spring完成後不會抱怨。一個定義會隱藏其他定義。但我找不到什麼是規則。
我想做這個測試目的。我使用基於註釋的配置來定義真實(生產)bean。然後我想使用特定於測試的XML配置文件來覆蓋這些定義並注入模擬bean。
編輯:由於你是幾個要求日誌,我花了一些時間創造一些。這裏是他們:
0 INFO org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org[email protected]3934f69a: startup date [Wed Mar 06 23:04:35 CET 2013]; root of context hierarchy
45 INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [applicationContext.xml]
223 INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Overriding bean definition for bean 'bean': replacing [Generic bean: class [org.example.AnnotatedBean]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [/Users/etienne/Documents/Développement/Registre/workspace/SpringPrecedence/target/classes/org/example/AnnotatedBean.class]] with [Generic bean: class [org.example.XmlBean]; 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 [applicationContext.xml]]
223 INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [otherApplicationContext.xml]
246 INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Overriding bean definition for bean 'bean': replacing [Generic bean: class [org.example.XmlBean]; 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 [applicationContext.xml]] with [Generic bean: class [org.example.ImportedXmlBean]; 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 [otherApplicationContext.xml]]
290 INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.s[email protected]6aba4211: defining beans [bean,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,aliasedBean,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
290 INFO org.example.AliasedBean - Construction of AliasedBean.
302 INFO org.example.Main - Application context loaded.
302 INFO org.springframework.context.support.ClassPathXmlApplicationContext - Closing org[email protected]3934f69a: startup date [Wed Mar 06 23:04:35 CET 2013]; root of context hierarchy
302 INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.s[email protected]6aba4211: defining beans [bean,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,aliasedBean,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
一些測試後,我發現我的上下文創建過程中得到一個例外,如果:
- 我有兩個
@Component("bean")
或 - 我有兩個
<bean id="bean"/>
元素相同 XML文件。
如果您使用的是特定於測試的配置,那麼還有哪些其他bean可以覆蓋? – 2013-03-05 20:49:13
基於註釋的配置bean。我的測試配置文件包含 ,因爲大多數bean都是真正的。只有少數需要被覆蓋。 –
2013-03-05 21:06:01
啓動日誌是否顯示任何內容?正如你所說,春天每個id只能有一個bean。這取決於它首先加載什麼,註釋類或xml。 – 2013-03-05 21:12:15