title: How do Spring annotations work?
body: <h2>Motivation</h2>
  
  <p>As a follow-up to my previous questions on classloading</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/15250656/how-is-the-classloader-for-a-class-chosen">How is the Classloader for a class chosen?</a></li>
  <li><a href="https://stackoverflow.com/questions/15248405/how-classloader-determines-which-classes-it-can-load">How Classloader determines which classes it can load?</a></li>
  <li><a href="https://stackoverflow.com/questions/15268057/where-does-bytecode-injection-happen">Where does bytecode injection happen?</a></li>
  </ul>
  
  <p>I'm curious about how do annotations work in a popular Spring framework. </p>
  
  <h2>Possible solution</h2>
  
  <p>As far as I understand, two mechanisms might be used:</p>
  
  <p><strong>1. Bytecode injection on classloading</strong></p>
  
  <p>Spring could use its own classloader to load required classes. At runtime, when the class is loaded and Spring determines it has some appropriate annotation, it injects bytecode to add additional properties or behavior to the class. </p>
  
  <p>So a controller annotated with <code>@Controller</code> might be changed to extend some controller base class and a function might be changed to implement routing when annotated with <code>@RequestMapping</code>.</p>
  
  <pre><code>@Controller
  public class HelloWorldController {
  
      @RequestMapping("/helloWorld")
      public String helloWorld(Model model) {
          model.addAttribute("message", "Hello World!");
          return "helloWorld";
      }
  }
  </code></pre>
  
  <p><strong>2. Reflection used for instantiation</strong> </p>
  
  <p><code>@Autowired</code> could be read by reflection at runtime by the BeanFactory to take care of the instantiation order and instantiate the configured properties.</p>
  
  <pre><code>public class Customer 
  {
      private Person person;
  
      @Autowired
      public void setPerson(Person person) {
          this.person = person;
      }
  }
  </code></pre>
  
  <h2>Question</h2>
  
  <p>How do Spring annotations really work?</p>