Maven and Spring

Thursday, 14 April 2011 09:08


Maven and Spring tutorial

This guide is a quick tutorial to getting started with Maven and Spring, and shows the making of an hello world sample

Tutorial requirements

Java JDK 1.5 or above (this is to execute Maven - it still allows you to build against 1.3 and prior JDK's)
Maven Apache Maven 2 installed and configured
Maven Basic Knowledge of Maven
Spring Spring Framework version 2.5.5

Maven Spring dependency

Maven spring framework dependency


<project>
	...
	<dependencies>
	...
    		<dependency>
      			<groupId>org.springframework</groupId>
	      		<artifactId>spring</artifactId>
      			<version>2.5.5</version>
	    	</dependency>
	</dependencies
</project>

Let's define a sample Hello world�Bean

Spring java source


package ch.javatutorial.spring;

public class HelloWorld {
	private String name;
	
	public void setName(String name)
	{
		this.name = name;
	}
	 
	public String getName()
	{
		return name;
	}	
}

Spring application context

We use the ApplicationContext.xml file to define some Java beans so that we can retrieve them in the application. In this way we can reimplement the classes and use alternatively different implementations by just changing the configuration in the ApplicationContext.xml file.

In this section we will wire up the components by defining the Application context definition file (application-Context.xml).

An ApplicationContext is the central interface to provide configuration for an application. An ApplicationContext provides the following functionalities:

  • Bean factory methods, inherited from ListableBeanFactory. This avoids the need for applications to use singletons.
  • The ability to resolve messages, supporting internationalization. Inherited from the MessageSource interface.
  • The ability to load file resources in a generic fashion. Inherited from the ResourceLoader interface.
  • The ability to publish events. Implementations must provide a means of registering event listeners. Inheritance from a parent context. Definitions in a descendant context will always take priority. This means, for example, that a single parent context can be used by an entire web application, while each servlet has its own child context that is independent of that of any other servlet.

Here is the code of our application-context.xml file :

Spring application context file


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
          "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="helloBean" class="ch.javatutorial.spring.HelloWorld">
    	<property name="name" value="Java-tutorial.ch"/>
  </bean>
</beans>

It should be located in src/main/resources according to the standard Maven directory layout

This application-context file set the name of the bean HelloBean with the value corresponding to the property name.

Spring integration test

Let's create an integration test loading the bean factory and asserting the expected result

You can find more detail on how to put in place integration tests on the link Maven Integration Tests
Spring integration test


package ch.javatutorial.spring;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class HelloWorldIntegrationTest {
	
	@Test
	public void testSpringInjection(){
		BeanFactory factory = new XmlBeanFactory(new ClassPathResource("application-context.xml"));
		HelloWorld hello = (HelloWorld) factory.getBean("helloBean");
		assertEquals("Name for bean helloBean not injected correctly1", "Java-tutorial.ch", hello.getName());
	}
} 
On your command line, execute the following Maven goal:
Maven spring tutorial


mvn test

The result should be displayed in the folder /target/failsafe-reports in the file ch.javatutorial.spring.HelloWorldIntegrationTest.txt and should look like :

Maven test console result


-------------------------------------------------------------------------------
Test set: ch.javatutorial.spring.HelloWorldIntegrationTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.389 sec 

Troubleshooting Spring Troubleshooting Spring

Spring invalid setter method Error �: Bean property 'name1' is not writable or has an invalid setter method

You probably try to define a property not present in the bean

Error application context cannot be opened Error �: class path resource [application-context.xml] cannot be opened because it does not exist

Spring is not able to find the application context file your defined using the XmlBeanFactory class

Tags: maven , tutorial , class , spring , bean , error , file , writable , opened

Add comment


Security code
Refresh