How to load multiple Spring application context files in a standalone Java application

If you ever need to load multiple Spring application context configuration files in a standalone Java application, here's some sample code that shows how to load them. In the following Java source code I load three Spring context/configuration files named dao1Context.xml, dao2Context.xml, and dao3Context.xml:

package com.devdaily.springtest1;

import com.devdaily.springtest1.dao.FileEventDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringMultipleApplicationContextExample
{

  //
  // define multiple spring application context files
  //
  private static final String[] SPRING_CONFIG_FILES = new String[]{"dao1Context.xml", "dao3Context.xml", "dao3Context.xml"};
  
  public static void main (String[] args)
  {
    new SpringMultipleApplicationContextExample();
  }

  public SpringMultipleApplicationContextExample()
  {
    //
    // load the spring application context files (multiple context files)
    //
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(SPRING_CONFIG_FILES);
  }

}

I trimmed a bunch of source code out of this example to focus on just loading these (multiple) Spring application context files, but hopefully I left enough that you can see how this is done. Simply create an array of Strings to contain the names of your context files, then use that String array as the argument to the Spring ClassPathXmlApplicationContext class constructor.