Software Architecture - Why do we use Layered Architectures?


Image


In this article, we explore the reasons why we build applications with several layers. We also look at different options for each layer in the Java world.

You will Learn

  • Why Have Multiple Layers?
  • What are typical layers and their responsiblities?
  • What are the typical frameworks used in each layer in the Java world?

Free Courses - Learn in 10 Steps

This is the fifth article in the following series of articles on Software Architecture :

Why Have Multiple Layers?

When we build any large application, such as a web application or service, we try to organize it into multiple layers. We could go for layers such as Web, Business, Access, among others:

image info

The reason we go to such lengths is a very important underlying principle : separation of concerns.

Each of the layers above have different responsibilities. Web layer is responsible for presenting information to the user. The Business layer is responsible for application’s business logic. The Data layer is responsible for taking care of the data access, and configuring and talking to the data stores. You might also have an additional access layer to talk to external applications, or queueing messages for dispatch.

By defining each layer to have a separate responsibility, you ensure high cohesion with-in each such layer.

Layers In Enterprise Java

When it comes to the Java world, the typical organization of enterprise applications follows this schematic:

image info

Implementing The Web Layer

Earlier, the web layer of a Java EE application is responsible for rendering the final view to the user with technologies like JSPs and variety of templating languages. With the advent of RESTful web services and evolution of JavaScript SPA frameworks, this has changed drastically.

Today, we expose a REST API from the Java Web layer, and a front-end framework (such as AngularJS or ReactJS) handles the user presentation.

The main responsibility of a web layer is to talk to business layer and send a proper response to REST API calls.

Another responsibility that a web layer typically handles is authentication and authorizations using a module like Spring Security.

image info

A few important decisions when designing your web layer are

  • Should the application have state? If yes, you need to store session information about the user. Ideally, you should not.
  • Which framework to use? The popular options are Spring Boot (MVC), JAX-RS(REST), JAX-WS(SOAP).

Implementing The Business Layer

image info

Lets look at the important business layer responsibilities:

Transaction Management

This is taken care of by Java Transaction API (JTA) and Spring Transactions.

Implementing The Access Layer

image info

The following are the responsibilities of the Access layer:

Communicating with Data Store

If your application communicates with an external database, JPA might be a good choice. If very complex database queries are needed, then you might want to use JDBC or MyBatis.

Spring Data JPA might be a good starting point for using JPA and Hibernate.

Communication With External Interfaces

This layer provides interface with the JMS module. It also communicates with AMQP implementations.

Other Layers

image info

One of the most important aspects that needs to be handled in any system is Cross Cutting Concerns. This includes tasks such as logging, performance and security.

Typically these are implemented using Aspect Oriented Programming. AspectJ and Spring AOP are options to consider.

Unit Testing

Unit testing of the application is another important concern. Typical frameworks that are provided include JUnit, Mockito and Spring Unit.

Do check out our video on this:

image info

Summary

In this article, we understood why we need to have multiple layers in an application. We looked at the layers in a typical Java web application, along with framework options available. We then had a look at the makeup of the individual Data, Business and Access layers of a typical Java web application.

Best Selling Udemy Courses

Image
Image Image Image Image Image Image Image Image Image

Join 450,000 Learners and 30+ Amazing Courses

350,000 Learners are learning everyday with our Best Selling Courses : Spring Boot Microservices, Spring, Spring Boot, Web Services, Hibernate, Full Stack React, Full Stack Angular, Python, Spring Interview Guide, Java Interview, Java Functional Programming, AWS, Docker, Kubernetes, PCF, AWS Fargate and Azure


Do not know where to start your learning journey? Check out our amazing learning paths:
Learning Path 01 - Spring and Spring Boot Web Applications and API Developer,
Learning Path 02 - Full Stack Developer with Spring Boot, React & Angular,
Learning Path 03 - Cloud Microservices Developer with Docker and Kubernetes,
Learning Path 04 - Learn Cloud with Spring Boot, AWS, Azure and PCF and
Learning Path 05 - Learn AWS with Microservices, Docker and Kubernetes


Subscribe

FREE COURSES



Related Posts

Writing Integration Tests for Rest Services with Spring Boot

Setting up a basic REST Service with Spring Boot is a cake walk. We will go one step further and add great integration tests!

Integrating Spring Boot and Spring JDBC with H2 and Starter JDBC

Learn using Spring Boot Starter JDBC to connect Spring Boot to H2 (in memory database) using Spring JDBC. You will create a simple project with Spring Boot. You will add code to the project to connect to a database using Spring JDBC. You will learn to implement the basic CRUD methods.

JUnit Tutorial for Beginners in 5 Steps

JUnit Tutorial for Beginners in 5 Steps. Setting up a basic JUnit example and understanding the basics of junit.

JPA and Hibernate Tutorial For Beginners - 10 Steps with Spring Boot and H2

JPA and Hibernate in 10 Steps with H2 - Setting up a basic project example with Spring Boot and in memory database H2. Its a cake walk.

Spring Boot Tutorial For Beginners in 10 Steps

Introduction to Spring Boot in 10 Steps. Learn the basics of Spring Boot setting up a basic project example with Spring Boot.

Spring Framework Tutorial for Beginners - Your First 10 Steps

Learn the basics of Spring Framework setting up a very simple example.

JPA and Hibernate Tutorial using Spring Boot Data JPA

Complete journey starting from JDBC to JPA to Spring Data JPA using an example with Spring Boot Data JPA starter project. We use Hibernate as the JPA Implementation.

Creating a Web Application with Spring Boot with JSP

Setting up a basic web application with Spring Boot is a cake walk. We will create a simple web application using Spring Initializr and add JSP features to it.

What is Spring Boot Auto Configuration?

Auto Configuration is the most important feature in Spring Boot. In this tutorial, we will learn important concepts about Auto Configuration with a couple of examples.

Unit Testing Rest Services with Spring Boot and JUnit

Setting up a Basic REST Service with Spring Boot is a cake walk. We will go one step further and add great unit tests to our RESTful Service.


Search