Get Started Using Spring MVC for Your Java EE Applications
- Get Started Using Spring MVC for Your Java EE Applications
- DispatcherServlet
- Home Controller / Home View
Spring is a lightweight, powerful, and flexible framework for building Java EE applications. It is easy to use and comes with its own Eclipse-based IDE called the SpringSource Tool Suite (STS). STS contains everything you need for building most Java EE applications. Also, JSON, AJAX, and jQuery are included libraries. It is nice because you don't have to add and remove several libraries to get one technology working with others.
Spring includes a set of modules, each with their own specific purpose (i.e., the JDBC module). This way, you can enable and disable different modules according to the needs of your application, thus keeping the framework as lightweight as possible.
While getting familiar with the Spring modules, the most important module is the Core module. This module provides the fundamental functionality of the spring framework. In this module, BeanFactory is the heart of any Spring-based application. The entire framework was built on the top of this module. This module makes the Spring container, which is similar to a JEE container, to manage transactions and object lifecycles.
The following defines the notable architectural features of Spring:
- Lightweight: Spring is lightweight when it comes to size and transparency. The basic version of the Spring framework is around 1MB. And the processing overhead is also very negligible.
- Inversion of control (IoC): Loose coupling is achieved in spring using the technique Inversion of Control. The objects give their dependencies instead of creating or looking for dependent objects.
- Aspect-oriented programming (AOP): Spring supports aspect-oriented programming and enables cohesive development by separating application business logic from system services.
- Container: Spring contains and manages the life cycle and configuration of application objects.
- Framework: Spring provides most of the intra-functionality, leaving rest of the coding to the developer.
Simple Hello World
Here’s a short tour to setting up a very simple Spring MVC application. Follow these steps:
Download STS from the Spring website.
One thing you may likely run into is some compile errors with the test server that comes with STS. The compile error occurs because a "backup" directory does not exist. You have to create the "backup" directory in C:\Program Files (x86)\springsource\vfabric-tc-server-developer-2.5.0.RELEASE\spring-insight-instance\ (example for Windows 7). After creating that directory, you'll need to give the windows user group write permission on the folder.
Generate scaffolding by using a template for a Spring MVC application.
If you've ever programmed in Ruby on Rails, you'll find Spring MVC is similar in a lot of ways. Like with Ruby, you can automate the scaffolding needed to organize your applications directory structure and basic setup. Spring does this through templates. The Spring MVC template sets up a simple Home Controller.
- Once the project is loaded, simply drag the project root folder on to the VMware vFabric Server. After the server starts and publishes the project, you should see the Hello World message using http://localhost:8080/springhello/ where springhello is your project name.
And that's it! Talk about simple-you can build a basic Spring-MVC application in only minutes. Let's go through what Spring set up for us and why.