Loading...

Chapter 4: Spring - Architecture

Introduction

The Spring Framework is based on a modular architecture that allows you to use only the parts you need for your application. The Spring architecture enables loose coupling through the use of Inversion of Control (IoC) and Dependency Injection (DI), making it easy to integrate with other frameworks and technologies.

In this chapter, we will explore the architecture of Spring, its components, and how they work together to make application development easier.


Key Components of Spring Architecture

  1. Spring IoC Container (Core Container)

    • IoC (Inversion of Control): The central concept of Spring. It refers to the Spring container taking over the responsibility of managing the objects in the application. Instead of creating objects manually, you let Spring create them for you and inject them where needed.
    • The IoC container is responsible for bean management, which means the creation, initialization, and configuration of beans (objects).

    The Spring IoC container is made up of several modules:

    • Core Container Modules: This includes the Core and Beans modules. It provides the fundamental features such as IoC and Dependency Injection.
    • ApplicationContext: An extension of the BeanFactory interface. It provides more features like event propagation, internationalization, and application layer-specific functionality.
  2. BeanFactory and ApplicationContext

    • BeanFactory: The most basic container, it’s responsible for reading configuration metadata and creating beans from the XML configuration or annotations.
    • ApplicationContext: This is the more advanced version of the BeanFactory. It extends the functionality of BeanFactory and adds several enterprise features, such as event handling, message resolution, and more.

    Difference Between BeanFactory and ApplicationContext:

    • BeanFactory is simpler and less feature-rich compared to ApplicationContext.
    • ApplicationContext is usually preferred because it provides more features.
  3. Spring AOP (Aspect-Oriented Programming)

    • AOP allows Spring to separate cross-cutting concerns (e.g., logging, transaction management, security) from the business logic.
    • Spring AOP allows you to define aspects, which are behaviors that can be applied to objects in your application.
    • Join points and advice: A join point is a point in the execution of the program, and advice is an action taken at a join point (e.g., logging or transaction management).
  4. Data Access/Integration Modules

    Spring provides modules for integrating with various data access technologies, such as:

    • JDBC (Java Database Connectivity): Simplifies working with relational databases.
    • ORM (Object-Relational Mapping): Integrates with frameworks like Hibernate or JPA for object-relational mapping.
    • JMS (Java Message Service): Supports messaging services.

    Spring’s transaction management module handles both programmatic and declarative transaction management.

  5. Web Modules

    • Spring MVC: A framework for building web applications following the Model-View-Controller design pattern.
    • Spring WebFlux: A reactive web framework that allows you to build non-blocking, event-driven applications.
  6. Spring Security

    • Provides security features such as authentication and authorization.
    • Supports multiple authentication methods, including form-based login, LDAP, and OAuth2.
  7. Spring Boot

    • Spring Boot simplifies the process of setting up and configuring a Spring application by providing sensible defaults and autoconfiguration.
    • Spring Boot Starter Projects: Pre-configured sets of dependencies for common tasks, such as web development or data access.

Spring Bean Lifecycle

The lifecycle of a Spring bean follows these general steps:

  1. Bean Creation: The Spring container creates an instance of the bean by calling its constructor (or using other methods like factory methods).
  2. Bean Initialization: After the bean is created, Spring performs initialization, which may involve setting properties, calling initialization methods, etc.
  3. Dependency Injection: Spring injects dependencies into the bean.
  4. Bean Destruction: When the application context is closed, Spring calls a destroy method on the bean to clean up resources.

Spring Architecture Flow

  1. Bean Configuration: In the first step, we define beans and configurations either through XML or annotations. Beans are created based on this configuration.
  2. IoC Container: The IoC container is responsible for managing beans. It is the central part of Spring’s architecture. When the application starts, the container initializes and injects dependencies into the beans based on their configuration.
  3. Dependency Injection: Spring uses Dependency Injection (DI) to automatically provide objects that a class needs. This ensures loose coupling, as the class does not need to create these objects itself.
  4. Aspect-Oriented Programming: Cross-cutting concerns like logging, security, etc., are handled using AOP. This allows developers to write cleaner code without duplicating concerns across the system.
  5. Data and Transaction Management: Spring integrates seamlessly with databases and handles transaction management to ensure data consistency.
  6. Web Layer: For web applications, Spring provides Spring MVC (Model-View-Controller) for request handling, view rendering, and managing business logic.

Spring Bean Scope

Spring beans can have different scopes, which define how they are created and managed by the container.

  1. Singleton: A single instance of the bean is created for the entire Spring container.
  2. Prototype: A new instance of the bean is created every time it is requested.
  3. Request: A new instance of the bean is created for each HTTP request (specific to web applications).
  4. Session: A new instance is created for each HTTP session.
  5. Global Session: A new instance is created for each global HTTP session (used in portlet-based applications).

Conclusion

In this chapter, we explored the architecture of the Spring Framework. We learned about the core components of Spring, such as the IoC container, AOP, Spring MVC, and Spring Security. We also discussed Spring’s modular nature and how these modules integrate seamlessly to create enterprise-level applications.

In the next chapter, we’ll delve deeper into the Spring Environment Setup, guiding you through the process of configuring your development environment to start working with Spring.