Spring application context example - using Spring in a standalone Java application

Spring application context FAQ: Can you provide an example of using a Spring application context file in a standalone Java application?

Sure. Here's an example of a simple Java program where I load a Spring application context file using the ClassPathXmlApplicationContext method. This is a typical way of loading the application context file in a Spring standalone application, i.e., something like a Swing application or some other standalone Java application (some form of client or server) started from a Java jar file.

In the example program shown below I load the Spring application context file (applicationContext.xml) almost immediately after the main method is started, and then I instantiate (create) a couple of Java beans whose definitions are stored in the Spring application context file.

The example Spring standalone application

Here's a simple example Java standalone program that loads my Spring application context file and creates a few beans from the application context file:

package com.devdaily.springtest1;

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

public class SpringApplicationContextExample
{

  public static void main (String[] args)
  {
    new Main();
  }

  public SpringApplicationContextExample()
  {
    // open/read the application context file
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

    // instantiate our spring dao object from the application context
    FileEventDao fileEventDao = (FileEventDao)ctx.getBean("fileEventDao");

    // create a FileEventType object from the application context
    FileEventType fileEventType = (FileEventType)ctx.getBean("fileEventType");

    // insert the file event with the spring dao
    fileEventDao.doInsert(fileEventType);
  }

}

The Spring application context file

This Java source code won't make a great deal of sense without also seeing the contents of the application context file that goes with this program, so here's the source code for the corresponding Spring applicationContext.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

  <bean id="fileEventType" class="com.devdaily.springtest1.bean.FileEventType">
    <property name="eventType" value="10"/>
    <property name="description" value="A sample description here"/>
  </bean>

  <bean id="fileEventDao" class="com.devdaily.springtest1.dao.FileEventDao">
    <property name="dataSource" ref="basicDataSource"/>
  </bean>

  <bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/my_database" />
    <property name="username" value="my_username" />
    <property name="password" value="my_password" />
    <property name="initialSize" value="3" />
    <property name="maxActive" value="10" />
  </bean>

</beans>

How the Spring application context file is loaded

Because I'm using the Spring ClassPathXmlApplicationContext method to load my applicationContext.xml file in this example application, all I have to do for this to work is make sure the applicationContext.xml file is in my classpath when I run my Java application. If your CLASSPATH includes your current directory there's really nothing you have to do.

(For a web application this is as simple as making sure this file ends up in the WEB-INF/classes folder, BUT, for web applications you don't really want to take this approach to loading your application context file. I'll discuss that approach in another tutorial.)

One other thing I should note is that some of this Spring application context file refers to a MySQL database setup, and requires the Apache Commons connection pooling library. If you're interested in seeing how this sample program works but you're not interested in connecting to a database, just remove all those lines and try to load your own beans from your own definitions.