blog‎ > ‎

A Minimal Java Application Sample Based on Spring 2.5 Framework

posted Mar 20, 2012, 1:49 PM by Markus Sprunck   [ updated Apr 28, 2013, 12:54 PM ]
By Markus Sprunck; Revision: 1.2; Status: final; Last Content Change: Apr 28, 2013;

This article describes a minimal standalone spring application on Spring 2.5.6 Framework. To start with Spring-Framework it is not necessary to use all parts of the framework and/or to work with a web application. The Spring-Framework can also used for small standalone applications. You need just some minutes setup your own application. 

In this tutorial Spring 2.5 with a pure XML configuration is described. The use of annotations is described in the article A Minimal Java Application Sample Based on Spring 3.1 Framework.

Sample Code

The following example demonstrates how easy it is to use Spring-Framework in a standalone application - just 4 files are needed:

// File #1: MinimalSpringApp.java

package com.sw_engineering_candies;

import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.context.ApplicationContext;

public class MinimalSpringApp {

private static final String CONFIG_PATH =                 "classpath*:com/sw_engineering_candies/application-config.xml";

static ApplicationContext springContext;

public static void main(String[] args) {
// get the spring context
springContext = new FileSystemXmlApplicationContext(CONFIG_PATH);
// get the bean
IFoo myBean = (IFoo) springContext.getBean("fooId");
// call a method of the bean
System.out.println(myBean.run("Hello World - "));
}

}

// File #2: application-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="fooId" class="com.sw_engineering_candies.Foo"></bean>

</beans>

// File #3: IFoo.java

package com.sw_engineering_candies;

public interface IFoo {

public String run(String input);

}

// File #4: Foo.java


package com.sw_engineering_candies;

public class Foo implements IFoo {

@Override
public String run(String input) {
return input + " from Foo";
}

}

If you run this small program, you should see something like this:

20.03.2012 20:57:41 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@d1e604:
    display name [
org.springframework.context.support.FileSystemXmlApplicationContext@d1e604]; 
    startup date [Tue Mar 20 20:57:41 CET 2012]; root of context hierarchy

20.03.2012 20:57:41 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from URL  
   [file:/C:/Projects/.eclipse3.7.rcp/MinimalSpringApp/bin/com/sprunck/sample/application-config.xml]

20.03.2012 20:57:41 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
   INFO: Bean factory for application context
   [org.springframework.context.support.FileSystemXmlApplicationContext@d1e604]:  
   org.springframework.beans.factory.support.DefaultListableBeanFactory@18aaa1e

20.03.2012 20:57:41 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
   INFO: Pre-instantiating singletons in 
   org.springframework.beans.factory.support.DefaultListableBeanFactory@18aaa1e:
   defining beans [fooId]; root of factory hierarchy

Hello World -  from Foo

Needed Libraries 

To bring all the files together, the following 4 libraries: 
are needed in the build path. 

Project Structure

Figure 1: Overview of the project in Eclipse

Find Code on GitHub

Change History

 Revision Date Author Description
 1.0 Mar 20, 2012 Markus Sprunck  first version
 1.1 Aug 19, 2012 Markus Sprunck improved layout for tablets
 1.2 Apr 28, 2013 Markus Sprunck source code now on GitHub

Top +1 Rated