title: What is the precedence of Spring beans definitions?
body: <p>When several Spring beans are defined with the same name, which one will hide the others?</p>
  <p>Let's say I have several classes annotated with <code>@Component(&quot;bean&quot;)</code> in the package org.example, plus an applicationContext.xml that contains:</p>
  <pre><code>&lt;context:component-scan base-package=&quot;org.example&quot;/&gt;
  &lt;alias name=&quot;aliasedBean&quot; alias=&quot;bean&quot;/&gt;
  &lt;bean id=&quot;aliasedBean&quot; class=&quot;org.example.AliasedBean&quot;/&gt;
  &lt;bean id=&quot;bean&quot; class=&quot;org.example.XmlBean&quot;/&gt;
  &lt;import resource=&quot;otherApplicationContext.xml&quot;/&gt;
  </code></pre>
  <p>Which bean will be retrieved when I do an <code>applicationContext.getBean(&quot;bean&quot;)</code>?</p>
  <p>According to the <a href="http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-beanname" rel="nofollow noreferrer">Spring documentation</a>:</p>
  <blockquote>
  <p>Every bean has one or more identifiers. These identifiers must be unique within the container that hosts the bean.</p>
  </blockquote>
  <p>However, I know (because I tested) that Spring won't complain when this is done. One definition will hide the others. But I couldn't find out what was the rule.</p>
  <p>I want to do this for testing purpose. I use annotation based configuration in order to define real (production) beans. Then I want to use a test-specific XML configuration file to override these definitions and inject mock beans.</p>
  <p>Edit: Since you were several to ask for logs, I spent some time creating some. Here are they:</p>
  <pre><code>  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
  </code></pre>
  <p>After some tests, I found out that I get an exception during context creation if:</p>
  <ul>
  <li>I have two <code>@Component(&quot;bean&quot;)</code> <em>or</em></li>
  <li>I have two <code>&lt;bean id=&quot;bean&quot;/&gt;</code> elements in the <em>same</em> XML file.</li>
  </ul>