Mastering Spring 5.0
eBook - ePub

Mastering Spring 5.0

  1. 496 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mastering Spring 5.0

About this book

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.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

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 of contents

  1. Title Page
  2. Copyright
  3. Credits
  4. About the Author
  5. About the Reviewer
  6. www.PacktPub.com
  7. Customer Feedback
  8. Preface
  9. Evolution to Spring Framework 5.0
  10. Dependency Injection
  11. Building a Web Application with Spring MVC
  12. Evolution toward Microservices and Cloud-Native Applications
  13. Building Microservices with Spring Boot
  14. Extending Microservices
  15. Advanced Spring Boot Features
  16. Spring Data
  17. Spring Cloud
  18. Spring Cloud Data Flow
  19. Reactive Programming
  20. Spring Best Practices
  21. Working with Kotlin in Spring

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Mastering Spring 5.0 by Ranga Rao Karanam in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming. We have over one million books available in our catalogue for you to explore.