title: In Spring, how do I use a ClassPathXmlApplicationContext to get an EntityManager?
body: <p>With Spring I can autowire a bean with the following property:</p>
  
  <pre><code>@PersistenceContext(unitName="foo") private EntityManager em;
  </code></pre>
  
  <p>Using the following I can manually autowire the bean "someBean":</p>
  
  <pre><code>ClassPathXmlApplicationContext ctx = 
        new ClassPathXmlApplicationContext("META-INF/applicationContext.xml");
  AutowireCapableBeanFactory fac = ctx.getAutowireCapableBeanFactory();
  fac.autowireBean(someBean);
  </code></pre>
  
  <p>However, I can't figure out how to directly get a particular EntityManager. The use case is that I want to write a test that will get all EntityManager objects and execute simple queries in them to ensure that they are set up properly. To do this I need to be able to get all EntityManager objects from the application context. How can I do that?</p>
  
  <p>The following does not work. It returns an empty map.</p>
  
  <pre><code>Map&lt;String,EntityManager&gt; ems = ctx.getBeansOfType(EntityManager.class);
  </code></pre>