Maven and Spring
Last Updated on Saturday, 25 June 2011 07:29
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
|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|
Let's define a sample Hello world Bean
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:
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.
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
The result should be displayed in the folder /target/failsafe-reports in the file ch.javatutorial.spring.HelloWorldIntegrationTest.txt and should look like :
You probably try to define a property not present in the bean
Spring is not able to find the application context file your defined using the XmlBeanFactory class