Simple Spring Annotation Configuration example

Keine Kommentare

This is the start of a tutorial session about the Spring framework. In the next episodes I try to introduce lessons learnd at our project Surpreso which works on Spring and Ruby on Rails. While most of the tutorials work on XML-configuration, this lesson focusses on configuration by annotated classes.

Complete sourcecode is avaiable on GitHub.

We will start by configurate the maven POM to include the neccessary Spring libraries.

<project xmlns="" xmlns:xsi=""
 <name>Surpreso Spring skeleton</name>


  <!-- Spring context -->
  <!-- Google guava library -->
  <!-- Apache Commons libraray -->

The first dependency loads the Spring context module, the central component of Spring. Google Guava and Apache Commons are just added for syntactic sugar.

At next we will care on the main configuration for our program. Therefore a package called config gets created and we will annotate our main configuration class. First one defines the class as configuration, second one loads the properties file from the class path.

package com.surpreso.spring_skeleton.confg;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@PropertySource({ "" })
public class MainConfig {


The file will be stored under src/main/resources in the maven way and simply holds a version information

version = 0.1

For default jobs we will write a little helping abstract job class, which initializes the Spring context and loads the configurations.


import org.apache.commons.lang3.ArrayUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.surpreso.spring_skeleton.confg.MainConfig;

 * An abstract Job class, which helps to initialize the Spring context.
 * @author Christoph Nagel
abstract public class AbstractSpringJob {

 static private ApplicationContext ctx;

  * Initiates the Spring context object by the given configurations. For
  * default, {@link MainConfig} is used for bootstrapping properties.
  * @param configurations
  *            An array of configuration classes
 static protected void init(Class<?>... configurations) {
  // create the Spring context
  ctx = new AnnotationConfigApplicationContext(
    (Class<?>[]) ArrayUtils.add(configurations, MainConfig.class));

  * Getter for the Spring context
  * @return The Spring context
 static protected ApplicationContext getContext() {
  return ctx;


Last but not least, a HelloWorldJob initializes the Spring context and prints the version of our first program.


 * A simple Hello World example how to initialize the Spring context-
 * @author Christoph Nagel
public class HelloWorldJob extends AbstractSpringJob {

 public static void main(String... arg) {
  // init the Spring context
  // do some job instructions
  System.out.println("Version: "
    + getContext().getEnvironment().getProperty("version"));


That's all. Next steps will be testing, Spring profiles and Web MVC

Keine Kommentare :

Kommentar veröffentlichen

Hinweis: Nur ein Mitglied dieses Blogs kann Kommentare posten.