Spring Maven integration

Here is the absolute minimal code to kickstart a Spring-enabled project using Maven.

Since it is impossible not to use the ApplicationContext interface in our Spring example we would need to include the spring-context Maven dependency and Maven should take it on from there bringing all the dependent jars based on the dependency hierarchy. In that case the POM should look like this:

<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.dimitrisli</groupId>
  <artifactId>helloSpring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
  	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-context</artifactId>
    	<version>3.0.6.RELEASE</version>
    </dependency>
  </dependencies>
</project>

Based on the dependency hierarchy, spring-context is depending on spring-aop, spring-beans, spring-core, spring-expression,and spring-asm. Spring-core is also depending on commons-logging which constitutes our absolute minimal jars we need to have:

  • spring-core
  • spring-asm
  • commons-logging
  • spring-context
  • spring-aop
  • spring-beans
  • spring-expression

The power of Maven dealing with the dependency hierarchy tree behind the scenes saves us from the hustle of dealing/defining explicitly these jars but in case we wanted to explicitly include all the spring related jars our POM would look like this:

<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.dimitrisli</groupId>
  <artifactId>helloSpring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
  	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>3.0.6.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-context</artifactId>
    	<version>3.0.6.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-expression</artifactId>
    	<version>3.0.6.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-aop</artifactId>
    	<version>3.0.6.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-asm</artifactId>
    	<version>3.0.6.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-beans</artifactId>
    	<version>3.0.6.RELEASE</version>
    </dependency>
  </dependencies>
</project>

We define the spring-beans.xml configuration file under /src/main/rousources and it looks like so:

<!--?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-3.0.xsd">
		
<bean id="myBeanTuple" class="com.dimitrisli.springFirstExample.MyBean" />

</beans>

This file creates for us a 1-to-1 mapping between the class MyBean (full namespace needed here) and the Spring bean called myBeanTuple.

MyBean is a trivial test class (not even a java bean) with its sole purpose in life to System.out.print a message within its only one method:

package com.dimitrisli.springFirstExample;

public class MyBean {

	public void printMyBean(){
		System.out.println("Inside My Bean");
	}
}

Everything is linked together in our BeanContainer that is loading the spring config file during runtime creating our application context and then claiming the bean from the context based on the name before calling the sole method to print our test string:

package com.dimitrisli.springFirstExample;

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


public class BeanContainer {

	public static void main(String[] args) {
		ApplicationContext context = new FileSystemXmlApplicationContext("/src/main/resources/spring-beans.xml");
		MyBean myBean = (MyBean) context.getBean("myBeanTuple");
		myBean.printMyBean();
	}
}

You can find the code in this github repository

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s