title: How to properly load and configure Spring beans from an external utility jar
body: <p>Currently I have a utility jar that contains a number of datastore services. Behind the scenes these datastore services use Spring Data MongoDB, and everything is configured using an app-context.xml file in the utility jar. I want this utility jar to be able to change the backing store without having to change anything that uses this utility jar.</p>
  
  <p>Now, I want to create a spring mvc web application that uses the datastore services from this utility jar.</p>
  
  <p>How do I set this up so that the spring mvc web app (or any other jar) can easily use the datastore services without having to know too much about the utility jar, but still have the beans in the utility jar loaded properly?</p>
  
  <p>I was thinking of adding a new java bean class to the utility jar that would load the app-context in it's own jar, and then set some properties on itself for the services. Then the spring mvc would create a bean using this new class in my utility jar, and reference the services through this bean.</p>
  
  <pre><code>/**
  * This bean would exist in the utility jar, and other jars/webapps would
  * create a new instance of this bean.
  */
  public class Services {
  
      private MyAService myAService;
      private MyBService myBService;
  
      public Services() {
         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("app-context.xml");
  
         // these are configured in the app-context.xml
         this.myAService = ctx.getBean("myAService");
         this.myBService = ctx.getBean("myBService");
      }
  }
  </code></pre>
  
  <p>Is this a good way to go about this? It seems like I would now have two spring application contexts, is that ok? How do I ensure that the proper app-context.xml is loaded, and not one from another jar? Is there a better way of doing this?</p>