Book Image

Learning Spring 5.0

By : Tejaswini Mandar Jog
Book Image

Learning Spring 5.0

By: Tejaswini Mandar Jog

Overview of this book

<p>Spring is the most widely used framework for Java programming and with its latest update to 5.0, the framework is undergoing massive changes. Built to work with both Java 8 and Java 9, Spring 5.0 promises to simplify the way developers write code, while still being able to create robust, enterprise applications.</p> <p>If you want to learn how to get around the Spring framework and use it to build your own amazing applications, then this book is for you.</p> <p>Beginning with an introduction to Spring and setting up the environment, the book will teach you in detail about the Bean life cycle and help you discover the power of wiring for dependency injection. Gradually, you will learn the core elements of Aspect-Oriented Programming and how to work with Spring MVC and then understand how to link to the database and persist data configuring ORM, using Hibernate.</p> <p>You will then learn how to secure and test your applications using the Spring-test and Spring-Security modules. At the end, you will enhance your development skills by getting to grips with the integration of RESTful APIs, building microservices, and doing reactive programming using Spring, as well as messaging with WebSocket and STOMP.</p>
Table of Contents (18 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
9
Explore the Power of RESTful Web Services

Problems addressed by Spring


Java Platform is a long term, complex, scalable, aggressive, and rapidly developing platform. The application development takes place on a particular version. The applications need to keep on upgrading to the latest version in order to maintain recent standards and cope up with them. These applications have numerous classes that interact with each other and reuse the APIs to take their fullest advantage to ensure that the application is running smoothly. However, this leads to some very common problems, such as scalability, plumbing code, boilerplate code, unavoidable non-functional code, and unit testing. Let's discuss these problems one by one.

Scalability

The growth and development of each of the technologies in the market is pretty fast, both in hardware as well as software. The application that was developed a couple of years ago may get outdated because of the growth. In the growing market, the hardware is revolving very fast. Starting from the RAM to hard disk capacity, headphones to bluetooth, and network to wireless communication, many things have been changed from the hardware point of view. Software is also not apart from the changes. The market is so demanding that the developers need to keep on changing the application on a frequent basis. This means, whatever the application that we develop today should be capable of handling the upcoming demands and growth without affecting the working application tomorrow. The scalability of an application is about handling or supporting the increased load of work to adapt to the growing environment instead of replacing them. Internet websites are a very simple example of what scalability means. We can call the website scalable when it supports the handling of increased traffic due to the increase in number of hits. As the code is tightly coupled, making it scalable becomes a problem.

Plumbing code

Let's take an example of configuring DataSource in the Tomcat environment. Now that the developers want to use this configured DataSource in the application, what will we do? Yes, we will do the JNDI lookup to get DataSource. In order to handle JDBC, we will acquire the connection using DataSource and then release the resources in try...catch. The code written in try...catch and the inter-computer communication using Socket are not application-specific code. Such a type of low-level code that helps you communicate between the application and its underlying layers is called plumbing code. The plumbing code is an unavoidable part of the application, without which, the system will not be able to work as per requirements. The plumbing code increases the length of the code and makes the debugging complex.

Boiler plate code

How do we get the connection while executing the JDBC code? We will need to register the Driver class and invoke the getConnection() method on DriverManager to obtain the Connection object. Is there any alternative to these steps? Actually, no! Whenever, wherever we have to perform JDBC operations, these same steps have to be repeated every time. This kind of repetitive code that developers need to write at many places with little or no modification to achieve some task is called the boilerplate code. The boiler plate code makes the Java development unnecessarily lengthier and complex.

Unavoidable non-functional code

Whenever application development happens, the developer concentrates on the business logic, look and feel, and persistency to be achieved. However, along with these things, the developers also give a rigorous thought on how to manage the transactions, handle the increasing load on the site, make the application secure, and much more. If we take a closer look, these things are not the core concerns of the application, but still, these are unavoidable. Such kind of code that is not handling the business logic (functional) requirement but is important for maintenance, troubleshooting, and managing security of an application is called a nonfunctional code. In most of the Java applications, along with core concerns, the developers have to write down nonfunctional code quite frequently. This leads to providing biased concentration on business logic development.

Unit testing of the application

Let's take an example. We want to test some code that is saving the data to the table in the database. Here, testing the database is not our motive; we just want to be sure whether the code that we have written is working fine or not. Enterprise Java application consists of many classes that are interdependent. As dependency exists in the objects, it becomes difficult to carry out the testing.

Spring mainly addresses the discussed problems and provides a very powerful yet easy solution. Let's discuss one by one how Spring provides this solution.

POJO-based development

In any application development, the class is a very basic structure where the developers do the coding. The class comprises of different methods. If the class is getting extended or implements an interface of the framework, reusing it becomes difficult as they are tightly coupled with API. Plain Old Java Object (POJO) is a very famous and regularly used terminology in the Java application development. Unlike Struts and EJB, Spring doesn't force developers to write the code that is importing or extending Spring APIs. The best thing about Spring is that developers can write the code that generally doesn't have any dependencies on the framework, and, for this, POJOs are the favorite choice. POJOs support loosely-coupled modules, which are reusable and easy to test.

Note

The Spring Framework is called to be non-invasive as it doesn't force the developer to use API classes or interfaces and allows us to develop loosely-coupled applications.

Loose coupling through the dependency injection

Coupling is the degree of knowledge one class has about the other class. When a class is less dependent on the design of any other class, it will be called loosely coupled. Loose coupling can be best achieved by interface programming. In the Spring Framework, we can keep the dependencies of the class separated from the code in a separate configuration file. Using the interfaces and dependency injection techniques provided by Spring, developers can write loosely-coupled code. (Don't worry; very soon, we will discuss dependency injection and how to achieve it.) With the help of loose coupling, one can write a code that needs a frequent change due to the change in the dependency it has. It makes the application more flexible and maintainable.

Declarative programming

In declarative programming, the code states what it will perform, but not how it will be performed. This is totally the opposite of imperative programming, where we need to state stepwise what we will execute. The declarative programming can be achieved using XML and annotations. Spring Framework keeps all configurations in XML from where it can be used by the framework to maintain the life cycle of a bean. As the development happened in Spring Framework, the 2.0 onward version gave an alternative to XML configuration with a wide range of annotations.

Boilerplate code reduction using aspects and templates

Earlier, we discussed that repetitive code is boilerplate code. The boilerplate code is essential, without which, providing transactions, security logging, and so on will become difficult. The framework gives a solution of writing an Aspect that will deal with such cross-cutting concerns, and there is no need to write them along with the business logic code. The use of Aspect helps us in the reduction of the boilerplate code, but the developers can still achieve the same end effect. The template for different requirements is another thing that the framework provides. Templates such as JDBCTemplate, HibernateTemplate have been provided by Spring, ensuring the reduction of the boilerplate code. However, as a matter of fact, you will need to wait to understand and discover the actual potential.

Layered architecture

Unlike Struts and Hibernate, which provide web persistency solutions respectively, Spring has a wide range of modules for numerous enterprise-development problems. This layered architecture helps the developer choose any one or more of the modules to write solutions for their application in a coherent way. For example, one can choose the Web MVC module to handle the web request efficiently, without even knowing that there are many other modules available in the framework.