Mastering Spring 5.0
eBook - ePub

Mastering Spring 5.0

Ranga Rao Karanam

Partager le livre
  1. 496 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

Mastering Spring 5.0

Ranga Rao Karanam

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

Develop cloud native applications with microservices using Spring Boot, Spring Cloud, and Spring Cloud Data FlowAbout This Book‱ Explore the new features and components in Spring‱ Evolve towards micro services and cloud native applications‱ Gain powerful insights into advanced concepts of Spring and Spring Boot to develop applications more effectively‱ Understand the basics of Kotlin and use it to develop a quick service with Spring BootWho This Book Is ForThis book is for an experienced Java developer who knows the basics of Spring, and wants to learn how to use Spring Boot to build applications and deploy them to the cloud.What You Will Learn‱ Explore the new features in Spring Framework 5.0‱ Build microservices with Spring Boot‱ Get to know the advanced features of Spring Boot in order to effectively develop and monitor applications‱ Use Spring Cloud to deploy and manage applications on the Cloud‱ Understand Spring Data and Spring Cloud Data Flow‱ Understand the basics of reactive programming‱ Get to know the best practices when developing applications with the Spring Framework‱ Create a new project using Kotlin and implement a couple of basic services with unit and integration testingIn DetailSpring 5.0 is due to arrive with a myriad of new and exciting features that will change the way we've used the framework so far. This book will show you this evolution—from solving the problems of testable applications to building distributed applications on the cloud.The book begins with an insight into the new features in Spring 5.0 and shows you how to build an application using Spring MVC. You will realize how application architectures have evolved from monoliths to those built around microservices. You will then get a thorough understanding of how to build and extend microservices using Spring Boot. You will also understand how to build and deploy Cloud-Native microservices with Spring Cloud. The advanced features of Spring Boot will be illustrated through powerful examples. We will be introduced to a JVM language that's quickly gaining popularity - Kotlin. Also, we will discuss how to set up a Kotlin project in Eclipse.By the end of the book, you will be equipped with the knowledge and best practices required to develop microservices with the Spring Framework.Style and ApproachThis book follows an end-to-end tutorial approach with lots of examples and sample applications, covering the major building blocks of the Spring framework.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Mastering Spring 5.0 est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Mastering Spring 5.0 par Ranga Rao Karanam en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Ciencia de la computaciĂłn et ProgramaciĂłn en Java. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2017
ISBN
9781787122338

Building a Web Application with Spring MVC

Spring MVC is the most popular web framework used to develop Java web applications. The beauty of Spring MVC lies in its clean, loosely coupled architecture. With a clean definition of roles for controllers, handler mappings, view resolvers, and Plain Old Java Object (POJO) command beans, Spring MVC makes use of all the core Spring features--like dependency injection and autowiring--to make it simple to create web applications. With its support for multiple view technologies, it is extensible too.
While Spring MVC can be used to create REST services, we discuss that in Chapter 5, Building Microservices with Spring Boot.
In this chapter, we will focus on reviewing the basics of Spring MVC with simple examples.
In this chapter will cover the following topics:
  • The Spring MVC architecture
  • The roles played by DispatcherServlet, view resolvers, handler mappings and controllers
  • Model attributes and session attributes
  • Form binding and validation
  • Integration with Bootstrap
  • Basics of Spring Security
  • Writing simple unit tests for controllers

Java web application architecture

The way we develop Java web applications has evolved during the last couple of decades. We will discuss the different architectural approaches to developing Java web applications and see where Spring MVC fits in:
  • Model 1 architecture
  • Model 2 or MVC architecture
  • Model 2 with Front Controller

Model 1 architecture

Model 1 architecture is one of the initial architecture styles used to develop Java-based web applications. A few important details are as follows:
  • JSP pages directly handled the requests from the browser
  • JSP pages made use of the model containing simple Java beans
  • In some applications of this architecture style, JSPs even performed queries to the database
  • JSPs also handled the flow logic: which page to show next
The following picture represents typical Model 1 architecture:
There are a lot of disadvantages in this approach, leading to quick shelving and the evolution of other architectures. A few important disadvantages are listed as follows:
  • Hardly any separation of concerns: JSPs were responsible for retrieving data, displaying data, deciding which pages to show next (flow), and sometimes, even business logic as well
  • Complex JSPs: Because JSPs handled a lot of logic, they were huge and difficult to maintain

Model 2 architecture

Model 2 architecture came in to solve the complexity involved with complex JSPs having multiple responsibilities. This forms the base for the MVC architecture style. The following image represents typical Model 2 architecture:
Model 2 architecture has a clear separation of roles between Model, View, and Controller. This leads to more maintainable applications. A few important details are as follows:
  • Model: Represents the data to be used to generate a View.
  • View: Uses the Model to render the screen.
  • Controller: Controls the flow. Gets the request from the browser, populates the Model and redirects to the View. Examples are Servlet1 and Servlet2 in the preceding figure.

Model 2 Front Controller architecture

In the basic version of Model 2 architecture, the requests from the browser are handled directly by different servlets (or Controllers). In a number of business scenarios, one would want to do a few common things in servlets before we handle the request. An example would be to ensure that the logged-in user has the right authorization to execute the request. This is a common functionality that you would not want to be implemented in every servlet.
In Model 2 Front Controller architecture, all requests flow into a single controller called the Front Controller.
Picture below represents typical Model 2 Front Controller architecture:
The following are some of the responsibilities of a typical Front Controller:
  • It decides which Controller executes the request
  • It decides which View to render
  • It provides provisions to add more common functionality
  • Spring MVC uses an MVC pattern with Front Controller. The Front Controller is called DispatcherServlet. We will discuss DispatcherServlet a little later.

Basic flows

Spring MVC uses a modified version of the Model 2 Front Controller architecture. Before we go into details about how Spring MVC works, we will focus on creating a few simple web flows using Spring MVC. In this section, we will create six typical web application flows using Spring MVC. The flows are listed as follows:
  • Flow 1: Controller without a View; serving content on its own
  • Flow 2: Controller with a View (a JSP)
  • Flow 3: Controller with a View and using ModelMap
  • Flow 4: Controller with a View and using ModelAndView
  • Flow 5: Controller for a simple form
  • Flow 6: Controller for a simple form with validation
At the end of every flow, we will discuss how to unit test the Controller.

Basic setup

Before we start with the first flow, we would need to get the application set up to use Spring MVC. In the next section, we will start by understanding how to set up Spring MVC in a web application.
We are using Maven to manage our dependencies. The following steps are involved in setting up a simple web application:
  1. Add a dependency for Spring MVC.
  2. Add DispatcherServlet to web.xml.
  3. Create a Spring application context.

Adding dependency for Spring MVC

Let's start with adding the Spring MVC dependency to our pom.xml. The following code shows the dependency to be added in. Since we are using Spring BOM, we do not need to specify the artifact version:
 <dependency> 
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
DispatcherServlet is an implementation of the Front Controller pattern. Any request to Spring MVC will be handled by the Front Controller, that is, DispatcherServlet.

Adding DispatcherServlet to web.xml

To enable this, we would need to add DispatcherServlet to web.xml. Let's look at how to do that:
 <servlet> 
<servlet-name>spring-mvc-dispatcher-servlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/user-web-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet...

Table des matiĂšres