Skip to the content.

Spring Web MVC Primer

-

##What we’ll cover:

-

The Java ecosystem is filled with frameworks, such as Jersey and RestEasy, which allow you to develop REST applications.

Spring Web MVC is one such popular web framework that simplifies Web and REST application development.

-

Spring

The Spring Framework has become a de facto standard for building Java/Java EE–based enterprise applications.

Originally written by Rod Johnson in 2002, the Spring Framework is one of the suite of projects owned and maintained by Pivotal Software Inc. (http://spring.io).

-

Spring Modules

-

Dependency Injection

-

At the heart of the Spring Framework lies Dependency Injection (DI).

As the name suggests, Dependency Injection allows dependencies to be injected into components that need them. This relieves those components from having to create or locate their dependencies, allowing them to be loosely coupled.

-

Dependency Injection: an example

To better understand DI, consider the scenario of purchasing a product in an online retail store:

Completing a purchase is typically implemented using a component such as an OrderService.

The OrderService itself would interact with an OrderRepository that would create order details in a database and a NotificationComponent that would send out the order confirmation to the customer.

-

Dependency Injection: an example

In a traditional implementation, the OrderService creates (typically in its constructor) instances of OrderRepository and NotificationComponent and uses them. Even though there is nothing wrong with this approach, it can lead to hard-to-maintain, hard-to-test, and highly coupled code.

-

DI, by contrast, allows us to take a different approach when dealing with dependencies.

With DI, you let an external process such as Spring create dependencies, manage dependencies, and inject those dependencies into the objects that need them.

So with DI, Spring would create the OrderRepository and NotificationComponent and then hand over those dependencies to the OrderService.

-

OrderService is now relieved of having to deal with OrderRepository/NotificationComponent creation, making it easier to test.

Each component can now evolve independently, making development and maintenance easier.

Also, it’s easier to swap these dependencies with different implementations, or use these components in a different context.

-

Spring Web MVC Overview

-

Spring Web MVC, part of the Spring Framework’s Web module, is a popular technology for building Web-based applications.

It is based on the model-view-controller architecture and provides a rich set of annotations and components. Over the years, the framework has evolved; it currently provides a rich set of configuration annotations and features such as flexible view resolution and powerful data binding.

-

Model View Controller Pattern

-

The Model View Controller, or MVC, is an architectural pattern for building decoupled Web applications. This pattern decomposes the UI layer into the following three components:

-

MVC

-

Spring Web MVC Architecture

-

Spring’s Web MVC implementation revolves around the DispatcherServlet—an implementation of the FrontController Pattern that acts as an entry point for handling requests.

-

MVC

-

-

-

-