title: Why is Spring's ApplicationContext.getBean considered bad?
body: <p>I asked a general Spring question: <a href="https://stackoverflow.com/questions/812178/auto-cast-spring-beans">Auto-cast Spring Beans</a> and had multiple people respond that calling Spring's <code>ApplicationContext.getBean()</code> should be avoided as much as possible.  Why is that?  </p>
  
  <p>How else should I gain access to the beans I configured Spring to create?</p>
  
  <p>I'm using Spring in a non-web application and had planned on accessing a shared <code>ApplicationContext</code> object <a href="https://stackoverflow.com/questions/812178/auto-cast-spring-beans/812291#812291">as described by LiorH</a>.</p>
  
  <p><strong>Amendment</strong></p>
  
  <p>I accept the answer below, but here's an alternate take by Martin Fowler who <a href="http://martinfowler.com/articles/injection.html#ServiceLocatorVsDependencyInjection" rel="noreferrer">discusses the merits of Dependency Injection vs. using a Service Locator</a> (which is essentially the same as calling a wrapped <code>ApplicationContext.getBean()</code>).</p>
  
  <p>In part, Fowler states, "<em>With service locator the application class asks for it [the service] explicitly by a message to the locator. With injection there is no explicit request, the service appears in the application class - hence the inversion of control.
  Inversion of control is a common feature of frameworks, but it's something that comes at a price. It tends to be hard to understand and leads to problems when you are trying to debug. So on the whole I prefer to avoid it [Inversion of Control] unless I need it. This isn't to say it's a bad thing, just that I think it needs to justify itself over the more straightforward alternative.</em>"</p>