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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.surpreso</groupId>
 <artifactId>spring-skeleton</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>Surpreso Spring skeleton</name>

 <properties>
  <spring.version>3.2.3.RELEASE</spring.version>
 </properties>

 <dependencies>
  <!-- Spring context -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <!-- Google guava library -->
  <dependency>
   <groupId>com.google.guava</groupId>
   <artifactId>guava</artifactId>
   <version>r09</version>
  </dependency>
  <!-- Apache Commons libraray -->
  <dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
   <version>3.1</version>
  </dependency>
 </dependencies>
</project>

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 application.properties from the class path.

package com.surpreso.spring_skeleton.confg;

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

@Configuration
@PropertySource({ "classpath:application.properties" })
public class MainConfig {

}

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

scr/main/resources/application.properties
version = 0.1

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

package com.surpreso.spring_skeleton.jobs;

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.

package com.surpreso.spring_skeleton.jobs;

/**
 * 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
  init();
  // 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