More‎ > ‎

Minimal Java Application Sample Based on Spring Framework

This short article describes a standalone application based on Spring 3 with annotations. 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.

Sample Code

The following example demonstrates how easy it is to use Spring-Framework in a standalone application - just 3 files are needed.  In the class MinimalSpringApp.java (lines 1-26) the application context is created in line 21 and the Spring configuration file application-config.xml is loaded from the class path. The getBean method loads a minimal test bean.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11

12
13
14
15
16
17
18
19
20
21

22

23
24
25
26
package com.sw_engineering_candies;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class MinimalSpringApp {

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

	@Autowired
	Foo myBean;

	void sayHello() {
		System.out.println(myBean.run("Hello World - "));
	}

	public static void main(final String[] args) {
		final ApplicationContext context = 
                        new ClassPathXmlApplicationContext(CONFIG_PATH);
		final MinimalSpringApp minimalSpringApp = 
                        context.getBean(MinimalSpringApp.class);
		minimalSpringApp.sayHello();
	}

}

The Spring configuration file application-config.xml is simple. Just two things have to be defined (i) the use of auto wiring (annotation-config) and (ii) the base package for Spring to find the Beans.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
 	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
   	http://www.springframework.org/schema/context
   	http://www.springframework.org/schema/context/spring-context-3.1.xsd">

	<context:annotation-config />
	<context:component-scan base-package="com.sw_engineering_candies" />

</beans>

In the class Foo.java a minimal bean is defined. The annotation in line 5 specifies the bean a Spring Service.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package com.sw_engineering_candies;

import org.springframework.stereotype.Service;

@Service
public class Foo {

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

}

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
21.06.2012 19:23:00 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ab50cd: 
    startup date [Thu Jun 21 19:23:00 CEST 2012]; 
    root of context hierarchy

21.06.2012 19:23:00 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from URL 
    [file:/C:/Projects/.eclipse3.7.rcp/minimal_spring_3.1.1_app/bin/com/sprunck/sample/
     application-config.xml]

21.06.2012 19:23:01 
org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1112783: 
    defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,
    org.springframework.context.annotation.internalAutowiredAnnotationProcessor,
    org.springframework.context.annotation.internalRequiredAnnotationProcessor,
    org.springframework.context.annotation.internalCommonAnnotationProcessor,foo,minimalSpringApp,    
    org.springframework.context.annotation.ConfigurationClassPostProcessor$
    ImportAwareBeanPostProcessor#0;root of factory hierarchy

Hello World -  from Foo

Needed Libraries

To bring all the files together, the following 6 libraries:
are needed in the build path. You may use a newer version of Spring and everything should work, but don't forget to update the new schema version in application-config.xml.

Project Structure

The structure of the eclipse project is simple. In the case your package name is different, don't forget to update the base-package in application-config.xml file.

Figure 1: Overview of the project in Eclipse

Find Code on GitHub

Sponsored Link