title: Why in Spring I am not allowed to annotate a final class with @Configuration?
body: <p>I am studying for the Spring Core certification and I have some doubts related to the answer of this question founded on the study material stuff.</p>
  
  <blockquote>
    <p><strong>Why are you not allowed to annotate a final class with @Configuration</strong></p>
  </blockquote>
  
  <p>My reasoning is the following one for substantiate this assertion:</p>
  
  <p>Consider the following configuration class:</p>
  
  <pre><code>@Bean
  public AccountRepository accountRepository() {
      return new JdbcAccountRepository(); 
  }
  
  @Bean
  public TransferService transferService() {
      TransferServiceImpl service = new TransferServiceImpl();
      service.setAccountRepository(accountRepository());
      return service;
  }
  
  @Bean
  public AccountService accountService() {
      return new AccountServiceImpl(accountRepository());
  }
  </code></pre>
  
  <p>At first look this situation could appear strange because the first method (<strong>accountRepository()</strong>) instantiates an <strong>JdbcAccountRepository</strong> object as a bean having <strong>id=AccountRepository</strong> that, following the Spring default behavior, is a <strong>singleton</strong></p>
  
  <p>The second and the third method call <strong>twice more time</strong> the <strong>accountRepository()</strong> method that should instantiate twice more <strong>JdbcAccountRepository</strong> objects and this is not possibile because it is singleton !!!</p>
  
  <p>So, to solve this situation Spring use the <strong>Inheritance-based Proxies</strong> strategy that expect to <strong>create a child class of my configuration class</strong> (the one annoted by <strong>@Configuration</strong>) and it is does:</p>
  
  <ul>
  <li><p><strong>For each bean, an instance is cached in the child class</strong></p></li>
  <li><p><strong>Child class only calls super at first instantiation</strong></p></li>
  </ul>
  
  <p>So the child class is the entry point because the following behavior is implemented by this child class:</p>
  
  <p>public class AppConfig$$EnhancerByCGLIB$ extends AppConfig {</p>
  
  <pre><code>public AccountRepository accountRepository() {
      // if bean is in the applicationContext
      // return bean
      // else call super.accountRepository() and store bean in context
  }
  
  public TransferService transferService() {
      // if bean is in the applicationContext, return bean
      // else call super.transferService() and store bean in context
  }
  
  .....................................................
  .....................................................
  .....................................................
  }
  </code></pre>
  
  <p>So if I annotate a configuration class with <strong>final</strong> Spring can't have this behavior because in Java <strong>a final class cannot be subclassed</strong></p>
  
  <p>Is it correct?</p>
  
  <p>Using the same reasoning can I also assert that in Spring <strong>I can't have a final method annoted with @Bean annotation</strong>?</p>
  
  <p>Because, as shown in the previous example, I have that when at startup time is created the child class (the proxy) of my configuration class happens that <strong>for each bean, an instance is cached in the child class</strong> and if it is final it is not possible (but I am absolutly not sure about this assertion)</p>
  
  <p>Am I missing something? Can you give me the exact explaination?</p>
  
  <p>Tnx</p>