title: Bean name resolution in Spring using @Bean and @Qualifier annotations
body: <p>I just found a behaviour of Spring that I cannot understand. I am using Spring Boot 1.5.x.</p>
  
  <p>In a configuration class, I declared two different beans.</p>
  
  <pre><code>@Configuration
  class Config {
      @Bean("regularRestTemplate")
      public RestTemplate regularRestTemplate(String toLog) {
          return new RestTemplateBuilder().setConnectTimeout(100)
                                          .setReadTimeout(100)
                                          .additionalInterceptors((request, body, execution) -&gt; {
                                              log.info("Inside the interceptor {}", toLog);
                                              return execution.execute(request, body);
                                          })
                                          .build();
      }
      @Bean("exceptionalRestTemplate")
      public RestTemplate regularRestTemplate() {
          return new RestTemplateBuilder().setConnectTimeout(100)
                                          .setReadTimeout(100)
                                          .build()
      }
  }
  </code></pre>
  
  <p>Then, I have a class that should use the bean called <code>exceptionalRestTemplate</code>.</p>
  
  <pre><code>@Component
  class Client {
      private RestTemplate restTemplate;
      @Autowired
      public Client(@Qualifier("exceptionalRestTemplate") RestTemplate restTemplate) {
          this.restTemplate = restTemplate;
      }
      // Code that uses the injected rest template
  }
  </code></pre>
  
  <p>Since I specified the name of the bean I want to be injected using the <code>@Qualifier</code> annotation, I would expect that Spring injects the bean called <code>exceptionalRestTemplate</code>. However, the bean called <code>regularRestTemplate</code> is actually used during the injection process.</p>
  
  <p>It turns out that the problem was in the name of the methods that declare the beans in the configuration class. Both are colled <code>regularRestTemplate</code>. Changing the second method name, solve the problem.</p>
  
  <p>My question is, why? I know that Spring uses the names of classes and methods annotated with the <code>@Bean</code> or with the <code>@Component</code>, <code>@Service</code>, etc... annotations to give a name to Java object inside the resolution map. But, I supposed that giving a name inside these annotations would override this behaviour.</p>
  
  <p>Does anybody tell me what's going on? </p>