Skip to main content

What is Spring Boot?

Objective​

This chapter aims to provide an extremely detailed understanding of Spring Boot, exploring not only its evolution and impact but also providing in-depth explanations of underlying concepts such as MVC, Tomcat, and Jackson. You will discover how and why Spring Boot has become an essential choice for Java application development, with a particular focus on its integration with these key technologies.


History and Context​

1. The Evolution of Java Development​

1.1 Java EE (J2EE) and its Challenges​

In the 1990s, Java 2 Enterprise Edition (J2EE) emerged as the standard for enterprise application development. J2EE provided a set of standardized specifications for developing robust and scalable software components, such as Enterprise JavaBeans (EJBs), servlets, and JSPs (JavaServer Pages). However, although powerful, J2EE suffered from heavy complexities. Configuring these components required deep expertise and considerable effort in terms of XML configuration and deployment. This complexity often discouraged small teams and lengthened development cycles, causing frustration and frequent errors among developers.

1.2 The Advent of Spring Framework​

Spring Framework was born in 2002, introduced by Rod Johnson in his book "Expert One-on-One J2EE Design and Development". This framework offered a lighter and more modular alternative to J2EE, focusing on concepts like Dependency Injection (DI) and Aspect-Oriented Programming (AOP). Spring facilitated the management of objects and dependencies while allowing greater flexibility in application design. However, as Spring Framework grew and gained features, the complexity of manual configuration (often via XML files) became an obstacle for developers working on large-scale projects.

1.3 The Introduction of Annotations in Java​

Annotations were introduced in Java with version 5 (J2SE 5.0) in 2004. They were designed to address several issues that became apparent with the growth of frameworks like Spring, where configuration via XML files became too cumbersome, repetitive, and error-prone. Before the introduction of annotations, developers had to manually configure objects, dependencies, and behaviors via external configuration files, mainly in XML. This approach had several drawbacks:

  • Complexity and Verbosity: XML files could become very large and complex, especially in large applications, making them difficult to manage and maintain.
  • Loose Coupling: XML files being separate from the source code introduced loose coupling between configuration and code, which could lead to inconsistencies and hard-to-debug errors.
  • Lack of Static Typing: XML configuration did not benefit from Java's static typing, meaning configuration errors could only be detected at runtime, not at compile time.

Annotations were introduced to solve these problems by allowing configuration directly in the source code, attaching metadata to Java classes, methods, and fields. This made configuration more concise, readable, and better integrated with the code.

1.4 The Birth of Spring Boot​

In 2014, to address the challenges posed by the growing complexity of configurations in Spring Framework, Pivotal Software introduced Spring Boot. Spring Boot was designed to simplify Java application development by providing a "convention over configuration" approach. This approach allows projects to start quickly with smart default settings while offering ready-to-use features for aspects like security, database management, and deployment. With Spring Boot, developers can focus on writing business code without being slowed down by complex configurations.


Key Features​

Spring Boot is rich in features that have contributed to its popularity and massive adoption in the software development industry. Among these, the following stand out:

2. Auto-configuration​

Spring Boot is distinguished by its auto-configuration capability. Unlike traditional frameworks, where each component must be configured manually, Spring Boot automatically detects the dependencies present in the project and applies the appropriate default configurations. This process relies on conditional annotations, such as @ConditionalOnClass and @ConditionalOnMissingBean, which check for the presence of certain classes or beans before applying the configuration.

Practical Example: If a Spring Boot application includes the spring-boot-starter-data-jpa dependency, the framework automatically configures a datasource, an EntityManager, and a transaction manager, allowing work with relational databases without additional configuration.

3. Grouped Dependencies (Starters)​

Spring Boot "starters" are sets of preconfigured dependencies for specific types of projects. They simplify dependency management by grouping the necessary libraries for a particular functionality. For example, the spring-boot-starter-web starter includes all the dependencies needed to develop a web application, such as Spring MVC, an embedded web server like Tomcat, and Jackson for JSON data handling.

Impact: Using starters reduces the risk of version conflicts and allows a project to start quickly with minimal configuration.

4. Embedded Web Server​

Spring Boot integrates a web server directly into the application, greatly simplifying deployment and development. By default, Spring Boot uses Tomcat as the embedded server, but it is possible to use other servers like Jetty or Undertow. This integration allows developers to deploy their applications as executable JARs, making applications more portable and simplifying their deployment across different environments.

Advantages:

  • Simplified Deployment: Applications can be run with a simple java -jar command, without complex external server configuration.
  • Portability: The application can be easily moved from one environment to another without requiring major modifications.

5. Production Ready Features​

With Spring Boot Actuator, developers have tools to monitor, manage, and diagnose their applications in production. Actuator exposes a series of endpoints that provide information on the application's state, performance metrics, and configuration properties in real-time. These features are essential for ensuring the robustness and availability of applications in production.

Importance: In a production environment, where performance and reliability are critical, these features allow proactive management and quick problem resolution.

6. Development Speed​

Spring Boot allows rapid development of Java applications thanks to its simplified configuration, starters, and auto-configuration. Projects can be set up in a few hours, allowing developers to focus on implementing business features rather than technical configuration.

Evolution of Practices: Spring Boot has encouraged the adoption of modern development practices, such as continuous integration (CI) and continuous deployment (CD), by simplifying the creation of production-ready applications from the early stages of development.

7. Community and Ecosystem​

Spring Boot benefits from the richness of the Spring ecosystem, which includes a wide range of complementary projects such as Spring Security, Spring Data, and Spring Cloud. This deep integration with other Spring projects, coupled with an active community and abundant documentation, makes Spring Boot a robust choice for enterprise application development.

Growth and Adoption: Since its introduction, Spring Boot has been adopted by companies of all sizes, quickly becoming a standard in Java application development.


Appendices​

A. MVC Model (Model-View-Controller)​

The MVC model is a software architecture that separates an application into three main components: the model (Model), the view (View), and the controller (Controller). This separation allows for clearer management of responsibilities, facilitating application development and maintenance.

  • Model: Represents the application's data and business logic. It interacts directly with the database and contains methods for retrieving, storing, and processing data.
  • View: Manages the presentation of data to the user. The view interacts with the model to obtain the data to display but does not contain business logic.
  • Controller: Links the model and the view. The controller receives user input, processes it (often by manipulating the model), and updates the view accordingly.

Integration with Spring MVC: Spring MVC implements the MVC model by facilitating the creation of controllers that manipulate models and select the appropriate views to return to the user.

B. Apache Tomcat Server​

Tomcat is an open-source servlet container developed by the Apache Foundation. It implements the servlet and JSP specifications of Java EE, allowing developers to deploy Java web applications on a lightweight application server.

  • Servlets: Small Java programs that run on the server and respond to HTTP requests by generating dynamic responses (often in HTML).
  • JSP (JavaServer Pages): A technology that allows Java code to be embedded directly in HTML pages, facilitating the generation of dynamic content.
  • Use with Spring Boot: Spring Boot integrates Tomcat by default as a web server, allowing easy deployment of web applications without requiring manual Tomcat configuration. When creating a Spring Boot web application, Tomcat is automatically configured and integrated into the application's JAR or WAR archive. This simplifies the development and deployment process, as the application can be started with a simple java -jar command, which launches both the application and the embedded Tomcat server.

C. Jackson: JSON Handling in Java​

Jackson is a powerful and popular Java library for handling JSON data. It allows serialization of Java objects to JSON and deserialization of JSON strings to Java objects. This functionality is essential in the development of RESTful APIs, where the JSON format is commonly used for data exchange between the client and server.

  • Serialization: Serialization involves converting a Java object into a JSON string. For example, an object representing a user with fields like name, first name, and email can be converted into an easily readable and transferable JSON string:

    {
    "nom": "Dupont",
    "prenom": "Jean",
    "email": "jean.dupont@example.com"
    }
  • Deserialization: Deserialization is the reverse operation, where a JSON string is converted into a Java object. This allows receiving data in JSON format from an HTTP request and transforming it into manipulable Java objects in the application.

Integration with Spring Boot: Spring Boot integrates Jackson seamlessly to handle JSON data. For example, when a controller method returns a Java object, Spring MVC uses Jackson to automatically convert it to JSON before sending it to the client. Similarly, HTTP requests with JSON bodies are automatically converted to corresponding Java objects by Jackson, facilitating JSON data handling in RESTful APIs.

Customization: Jackson offers many customization options to handle complex scenarios, such as ignoring null properties, formatting dates, or managing reference cycles. These customizations can be configured via annotations or specific configurations in Spring Boot.


Conclusion​

Spring Boot is much more than just an extension of Spring Framework. It is a powerful platform that has transformed the way Java developers design, deploy, and maintain applications. By simplifying configuration, integrating production-ready tools, and offering great flexibility, Spring Boot has enabled developers to create modern and scalable Java applications with unparalleled efficiency. Its massive adoption testifies to its significant impact on the software development industry.