Spring bean定义的优先级是什么?

当使用相同的名称定义多个Spring bean时,哪一个会隐藏其他的?

假设我在包org.example中有几个用@Component("bean")注释的@Component("bean") ,另外还有一个applicationContext.xml,它包含:

      

当我执行applicationContext.getBean("bean")时将检索到哪个applicationContext.getBean("bean")

根据Spring文档 :

每个bean都有一个或多个标识符。 这些标识符在托管bean的容器中必须是唯一的。

但是,我知道(因为我测试过),Spring完成后不会抱怨。 一个定义将隐藏其他定义。 但我无法找出规则是什么。

我想这样做是为了测试目的。 我使用基于注释的配置来定义真实(生产)bean。 然后我想使用特定于测试的XML配置文件来覆盖这些定义并注入模拟bean。

编辑:由于你有几个要求日志,我花了一些时间创建一些。 他们是:

  0 INFO org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@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.springframework.beans.factory.support.DefaultListableBeanFactory@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.springframework.context.support.ClassPathXmlApplicationContext@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.springframework.beans.factory.support.DefaultListableBeanFactory@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 

经过一些测试后,我发现在上下文创建期间我遇到exception,如果:

  • 我有两个@Component("bean")
  • 我在同一个 XML文件中有两个元素。

  • Bean按xml定义文件中的顺序注册。

  • 扫描的bean在找到xml标记的位置注册,但扫描的bean不能覆盖以前注册的bean定义。

  • 如果DefaultListableBeanFactory.allowBeanDefinitionOverriding为true(默认情况下),则Xml bean定义可以覆盖任何以前的bean定义。

所以XML胜利了。

如果先放置组件扫描标记,则xml bean将覆盖扫描的标记。 如果你把它放在最后,扫描的bean将被忽略。

编辑

如果在bean定义中的name属性中声明或使用别名标记声明别名,则别名具有不同的行为。

  • 使用alias标记声明的别名会隐藏任何后面的bean定义,并使用相同的名称。
  • name属性中声明的别名通过抛出BeanDefinitionParsingException来阻止任何其他bean定义使用相同的名称。

例如:

    -- throw Exception (name bar is in use) 

    -- Hidden by alias no exception thrown 

不同之处在于BeanDefinitionParserDelegate在bean元素嵌套的相同bean级别保存了一个名称和别名列表,并在解析bean定义时检查名称唯一性。

别名标记由DefaultBeanDefinitionDocumentReader.processAliasRegistration()直接处理,解析器委托不知道此名称。

我不知道这是一个bug还是故意但是引用没有说明任何内容,似乎预计别名的内部和外部声明具有相同的行为。