Understanding Code Structure for Spring Boot

Learn via video courses
Topics Covered

Overview

The application's maintainability can be easily judged by how the code is structured. Code in which classes are scattered without restriction leads to a complex and tangled structure and directly affects the manageability and releases.

Such a codebase is called "spaghetti code".

code-structure-for-spring-boot

Therefore it's essential to structure code well from the beginning.

Introduction

Though spring boot does not advocate any specific structure, some best practices help the developer to create manageable code.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Best Practices for Structuring Code

About Typical Layout

A typical layout for maven projects contains different directories, each for specific types of files to be placed.

about-typical-layout

  • src/main/java:
    Package should contain source java classes. We should not create classes directly in this folder; rather, we should have our package. For example, we have the com.scaler.ems package under this folder. Typically package name is the same as that of the group id.

    about-typical-layout-1

  • src/main/resources:
    This folder should contain configuration files, properties files, yml files, and other such configurations.

  • src/test/java:
    This folder should contain unit test classes.

  • src/test/resources:
    This folder should contain configuration files used in tests.

  • target:
    This folder contains compiled classes and generated project artifacts.

Using the “default” Package

A class not part of any package is in a default package. For example, any class in src/main/java will go into the default package.

Use of default package is discouraged and should be avoided as it may cause issues in automatic bean scan.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Locating the Main Application Class

Spring recommends keeping the runner class in the root package, and actual business logic classes should be kept in sub-packages. There are a couple of advantages to it.

  1. It can automatically identify and scan @Entity classes for a JPA project.
  2. All the beans in the sub-package will be automatically discovered without any additional @ComponentScan.

locating-the-main-application-class

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Approaches in Layout Structure

We can structure your code in two ways:

  1. Structure by layer
  2. Structure by feature.

It affects the application's cohesion and coupling factors.

A good structure has low coupling and high cohesion.

Let's understand each of them.

Structure by Layer

In this structure, classes are placed in the package based on the layer it belongs to. This method causes low cohesion within packages because packages contain classes that are not closely related. Below is an example of packaging by layers:

The typical structure looks like the one below.

On observing this structure, we see that there are:

  • High coupling:
    Because classes tend to change in all the packages to implement the functionality.
  • Low cohesion:
    Because each package contains unrelated code.

This structure loses coupling and cohesion.

Structure by Feature

In this structure, packages contain all classes required for a feature. The feature-based packing ensures that closely related classes are kept together. An example of this structure is given below:

On observing this structure, we see that there are:

  • Low coupling:
    Only classes within the package is touched to implement the functionality.
  • High cohesion:
    Set of related classes is kept together as they usually get changes.

Further, this structure aligns well with the microservices principle.

Conclusion

In this article, we have covered.

  • Good practice for structuring the code
  • Advantages and disadvantages of different structuring styles.
  • Spring boot recommended code and package structure.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more