Spring Framework
Spring is an open source frame work for building enterprise java applications. Spring contains spring core and collection of projects. Spring Core, often referred to as the core module of the Spring Framework, is the foundational module that provides the essential components and features for building Java applications. Spring Core serves as the foundation for other modules within the Spring ecosystem, such as Spring MVC (Model-View-Controller) for web applications, Spring Data for data access, and more.
The key concepts of spring core are:
- Inversion of Control (IoC): Spring Core implements the principle of Inversion of Control (IoC), which means that the control of object creation and management is shifted from the application code to the Spring container. This promotes loose coupling and easier maintenance of code.
Dependency Injection (DI): Dependency Injection is a core concept in Spring Core. It allows developers to inject dependencies into classes rather than hard-coding them, promoting flexibility and easier testing.
Container: The Spring IoC container is at the heart of Spring Core. It manages the lifecycle of Java objects (beans) and applies IoC to enhance the modularity and scalability of applications.
Beans: In Spring, a bean is an object that is instantiated, assembled, and otherwise managed by the Spring IoC container. Beans are defined in the Spring configuration and can be Java objects or third-party objects configured and managed by Spring.
ApplicationContext: The
ApplicationContext
is a central and advanced interface in the Spring Framework that is responsible for managing the lifecycle of beans (objects) within a Spring application. It is an advanced form of the IoC container.AOP (Aspect-Oriented Programming): Spring Core supports Aspect-Oriented Programming, allowing developers to modularise cross-cutting concerns like logging, security, and transactions. AOP complements the traditional Object-Oriented Programming (OOP) paradigm.
Validation and Data Binding: Spring Core provides support for data validation and binding, allowing developers to validate and bind user input to Java objects.
Event Handling: Spring Core supports a robust event handling mechanism, allowing communication between decoupled components within the application.
Spring Boot:
- A project that simplifies the process of building, deploying, and running Spring applications. It provides a set of conventions, defaults, and auto-configurations to streamline development.
Spring Data:
- A set of projects that simplify data access in Spring applications. It provides consistent and familiar programming models for various data sources, including relational databases, NoSQL databases, and more.
Spring Security:
- A framework for implementing authentication, authorisation, and protection against common security vulnerabilities in Spring-based applications.
Spring Cloud:
- A set of tools and frameworks for building distributed and cloud-native applications. It includes projects for service discovery, configuration management, load balancing, and more.
Spring Integration:
- A framework for building enterprise integration solutions. It facilitates the exchange of information between different systems and applications.
Spring Batch:
- A framework for building batch processing applications. It simplifies the development of robust and scalable batch processing tasks.
Spring Web:
- Includes various projects related to web development, such as Spring MVC for building web applications, Spring WebFlux for reactive programming, and Spring Web Services for creating web services.
Spring Mobile:
- A project for simplifying the development of mobile web applications.
- Spring Social:
- Integrates with popular social media platforms, providing a framework for social login, sharing, and interaction.
Spring HATEOAS:
- Enables the creation of RESTful APIs following the principles of HATEOAS (Hypermedia as the Engine of Application State).
Spring Shell:
- A framework for building command-line applications.
Spring Kafka:
- Integrates the Spring framework with Apache Kafka, providing support for building Kafka-based messaging systems.
Spring Data Flow:
- A cloud-native orchestration service for building data integration and real-time analytics applications.
Spring Cloud Data Flow:
- An extension of Spring Cloud for building and orchestrating data micro-services.
Spring Cloud Sleuth:
- Provides distributed tracing for micro-services architecture.
Spring Test:
- Includes projects and modules for testing Spring applications, such as Spring TestContext Framework and Spring Boot Testing.
In order to set Spring Bean’s scope, we can use @Scope annotation or “scope” attribute in XML configuration files. There are five supported scopes:
- Singleton
- Prototype
- Request
- Session
- Global-session
Spring Bean Life Cycle
Instantiation:
- The process starts with the creation of a bean instance. This can happen through constructor invocation or through a factory method, depending on the bean configuration.
Populating Properties:
- Once the bean is instantiated, Spring sets its properties and dependencies. This process is known as dependency injection, where the framework injects values into the bean's properties.
Bean Post-Processing (Initialization):
- Spring provides hooks for custom initialization logic through methods annotated with
@PostConstruct
or implementing theInitializingBean
interface. These methods are called after the bean has been instantiated and its dependencies have been injected. Custom Initialization (InitializationCallback):
- If you prefer programmatic initialization, you can implement the
InitializationCallback
interface and register it in the bean definition.
- Bean Ready for Use:
- At this point, the bean is fully initialized and ready for use. It can be injected into other beans or accessed by other parts of the application.
- Bean Post-Processing (Destruction):
- When the application context is shut down or when explicitly triggered, beans can go through a destruction phase. This allows for releasing resources, closing connections, or performing cleanup operations. Custom destruction logic can be added through methods annotated with @PreDestroy or implementing the DisposableBean interface.
Custom Destruction (DestructionCallback):
- If you prefer programmatic destruction, you can implement the
DestructionCallback
interface and register it in the bean definition.
- In a nutshell, Spring IoC Container first check for dependency then calls its constructor. After creation of bean, it first injects the dependencies auto wired 0n constructor, Field and setters. Then Aware interfaces(BeanNameAware, BeanClassLoaderAware, ApplicationContextAware…) and annotated method like PostConstruct is invoked. At last init-method is invoked.
Comments
Post a Comment