title: Performance of Spring @Autowire vs getBean(className)
body: <p>There are two variants of code:</p>
  
  <pre><code>public class MyClass {
   public void myMethod() {
    AnotherClass object = SpringContexHolder.getContext().getBean(AnotherClass.class);
    object.doSomething();
   }
  }
  
  @Component
  public class MyClass {
  @Autowired
  AnotherClass object;     
  
  public void myMethod() {
   object.doSomething();
  }
  }
  </code></pre>
  
  <p>Will there be any performance penalties in the first variant (by the way in the first place it's not spring bean, just simple class)?
  Does Autowiring do the same as getBean?</p>
  
  <p>P.S. I think I should expand my question a little. The situation is that team I joined uses Spring injection in the project only via getBean(className). The reason I guess is that most of project classes already written are not Spring beans and using autowiring in one class means usually making dependent class bean as well and so on until most of the classes become beans...</p>
  
  <p>Ok, I think I understand testability penalties and overall lacking of code-style of such an approach. But isn't there a perfomance penalty alsо? Isn't there difference between performance of ready-to-go Spring singleton that is constructed on startup with all it's fields autowired and invoking getBean(classname) (afaik several times slower then pure hashmap.get(object)) from non-spring non-singleton class (ecpecially in critical places)?</p>
  
  <p>P.S.S. I created smth like mini-benchmark (I understand that it's hard to get real info because of work of GC, JIT and so on but nevertheless...).
  My results are (the bigger numbers - the worse):
  Autowiring time - 193, GetBean time - 2161, Method in the same class - 173, Static method in another class - 206</p>