Setting up the development environment
Setting Up Java Development Kit (JDK)
You can download the latest version of SDK from Oracle’s Java site http://www.oracle.com/technetwork/java/javase/downloads/index.html.
Once JDK is installed successfully set PATH and JAVA_HOME environment variables to refer to the directory, typically Java_installation_directory/bin and Java_installation_directory respectively. It varies on the operating system whether you are using windows or Linux. I will not be going into detail on this because I assume readers will have this knowledge already.
Alternatively, if you use an Integrated Development Environment (IDE) like Eclipse (STS), go to the preference page and do the necessary setting.
Setting up STS/Eclipse
At the time of writing this book, STS version 2.9.1.RELEASE was the version which was freely available to all developers to download and use.
Even though STS is built on top of Eclipse technology stack, if you find using Eclipse more convenient instead of STS, please continue using this for your Spring application development as well. There are various plug-in available for standard Eclipse platform which will aid in Spring application development.
Please follow the installation instructions provided at http://download.springsource.com/release/STS/doc/STS-installation_instructions.pdf and choose which installation is best for your purpose. There are two ways of installation: first one via distribution and second one via update site.
Setting up Spring Framework
Step 1: Download the latest version of Spring Framework binaries from http://www.springsource.org/download.
Step 2: At the time of writing this tutorial, latest GA release for Spring Framework was 3.1.1.RELEASE. Download appropriate zip file onto your machine. When you unzip the downloaded file it will give you directory structure inside as follows:
Figure 7 Directory structure of Spring Framework distribution
You will find all the Spring libraries in the directory spring-Framework-3.1.1.RELEASE\dist. Make sure you set your CLASSPATH variable on this directory. If you are using Eclipse (STS) then it is not required to set CLASSPATH because all the setting will be done through IDE. Once you are done with this last step, you are ready to do your first Spring Application.
Setting up Maven for Building the project
You need to have a build Framework in your project for build the project and to automatically executing the unit tests in your application. Ant and Maven are good build Frameworks which can do this for you. Maven is according to me is the build Framework which does something more than what Ant does for you. Almost all of open source projects prefer Maven against Ant nowadays. Due to the same reasons; I have used Maven to do this in our sample application.
To get Maven, go to http://maven.apache.org/download.html and download the latest version. You then need to perform two other tasks as follows to complete the installation (the second task is optional though):
- Add the Maven’s bin directory to the system or command line path.
- Increase the memory available to Maven by doing the following in your setting MAVEN_OPTS -Xms128m -Xmx512m
You can always get the complete installation and setup details from maven projects home page http://maven.apache.org/.
The sample application
This provides an overview of the domain and the application you will be working throughout this book. I will try to use code snippets taken from this sample application as much as possible so that at any point you can go to and fro between the book and the actual working application. In practice most things are highly configurable and the Framework provides many powerful services. The underlying classes can also be easily extended to adapt their behavior. Many Spring Beans will be created behind the scenes by the configuration itself; to begin with you don’t need to know nitty-gritty’s of these.
Requirements and Domain Overview
The sample application is a very basic Spring MVC application which has some JSP accessed through a web controller. We will be using airline domain. As always requirements given by customers are one liner. A typical business requirement is as below:
“I am doing business on airline cargo. I would like to reward my customers for choosing me to do their business. For various parameters on which they give me business I would like to give them points (loyalty), which they can redeem it at a later point for doing more business with us. Build me a system to do this and make sure it’s easy to use and intuitive by nature. Since my business is dynamic I would like to have the application also to be dynamic and ready to change.”
Cool, build the system.
Note: We will not go into solving all the use cases coming out of this statement of requirement but will try to solve certain core use case and try to increase the complexity of the application by introducing the power of Spring and how well it adapts and how well it could be extended. For whatever features Spring Framework provides by default, we will use it without any change at all. For example, for authentication and authorization, we will completely rely on Spring Security and so on.
Various entities are:
- Shipment (Cargo)
- Customer Account
- Customer Points
- Shipment Weight Points
A customer while booking a shipment (cargo) will get loyalty points on various parameters of the Shipment and Customer which will get these points credited in his customer account against a Shipment reference. The points can be segregated into customer parameter and shipment parameter based points. Unique reference number will be there for a customer, shipment and customer account.
Figure 8 Objects working together to get the loyalty points for a shipment
Shipment and Customer details are stored in a persistent form inside a relational database. The LoyaltyService implementation delegates to supporting data access services called ‘Repositories’ to load Shipment and Customer objects from their relational representations. A ShipmentRepository is used to find a Shipment by its reference number. A CustomerRepository is used to find a Customer by its customer number. A CustomerAccountRepository is used to credit the loyalty points against a customer.
The full calculateLoyaltyPointsAndCreditAccount() sequence incorporating these repositories is shown below:
Figure 9 The complete calculateLoyaltyPointsAndCreditAccount() sequence
Loyalty Points Database Schema
Figure 10 Loyalty Points Database Schema
Figure 11 Spring Security Authentication and Authorization Default Tables
Page Visitors: 7143