Spring is likely the most popular Java framework for creating simple web applications, enterprise-level cloud applications, and microservices. Spring provides comprehensive infrastructure support for application development with complex requirements and designs.
The Spring framework handles infrastructure (via dependency injection), allowing us to concentrate on the application's core functionality. Spring framework contains formalized design patterns as first-class objects, which we can integrate into our application(s) without worrying too much about their backend functionality.
The Spring Framework is composed of individual modules. Depending on the functionality they require, applications can select the modules they need.
As illustrated in the following diagram, Spring modules are categorized as Core Container, Data Access/Integration, WebMVC, AOP (Aspect Oriented Programming), Instrumentation, Messaging, Testing, and others.
The following is a subset of the advantages of using Spring Framework.
In software engineering, inversion of control (IoC) is a programming technique in which object coupling is bound at run time by an assembler object and is typically unknown at compile time through static analysis.
In conventional programming, the flow of business logic is determined by objects that are statically assigned to each other.
Using inversion of control, the application flow is dependent on the object graph instantiated by the assembler and is enabled by object interactions defined through abstractions. "dependency injection" is used to accomplish the binding process.
Inversion of control is a design paradigm used to give more control to the components of an application that are actually performing the work.
Dependency injection is a design pattern used to create instances of objects on which other objects depend without knowing at compile time which class will provide that functionality.
Inversion of control is dependent on dependency injection because a mechanism is required to activate the components that provide the specific functionality. If the framework is no longer in charge, how will it know which components to create?
Spring allows for the following methods of dependency injection:
- Constructor injection, Setter injection, Fields Injection
In the spring framework, inversion of control refers to the capability of the framework to create, destroy, and manage the beans' lifecycle.
Spring's inversion of control is centered on the ApplicationContext (which uses the BeanFactory internally).
The Spring bean factory assembles beans using configuration metadata, which can take the form of XML configuration or annotations.
A BeanFactory is comparable to a factory class that stores a collection of beans. BeanFactory stores bean definitions within itself and instantiates a bean whenever a client requests one.
BeanFactory is capable of establishing associations between cooperating beans as they are instantiated. BeanFactory participates in the life cycle of the beans by invoking initialization and destruction methods.
Application context and bean factory are equivalent at a high level. Both load bean definitions, link beans together, and dispense beans upon demand. In addition, the application context offers:
There are three ways to configure Spring in applications:
External configuration files, typically in XML format, are used to specify Spring framework's required dependencies and services. These XML configuration files typically begin with the beans> tag and include numerous bean definitions AND application-specific configuration options.
Spring XML Configuration's primary objective is to configure all Spring components with XML files.
This indicates that no other Spring Configuration will be present (like annotations or configuration via Java classes).
A Spring XML Configuration uses Spring namespaces to make the sets of XML tags used in the configuration accessible; the primary Spring namespaces are context, beans, JDBC, TX, AOP, MVC, etc.
@Configuration annotated classes and @Bean annotated methods form the core of Spring's java-configuration support.
The @Bean annotation indicates that a method creates, configures, and initializes a new object managed by the Spring IoC container. The @Bean annotation provides the same functionality as the <bean/> element.
Annotating a class with @Configuration signifies that its primary function is to provide bean definitions. In addition, @Configuration classes permit inter-bean dependencies to be defined by calling other @Bean methods within the same class.
Beginning with Spring 2.5, dependency injection could be configured using annotations. Therefore, instead of using XML to describe a bean's wiring, we can move the bean configuration directly into the component class by using annotations on the relevant class, method, or field declaration.
Annotation injection occurs prior to XML injection; therefore, the XML configuration will supersede the annotation configuration when beans are wired using both approaches.
Spring's default configuration does not include annotation wiring. Therefore, prior to utilizing annotation-based wiring, we must enable it in our Spring configuration file.
It is simple to comprehend the life cycle of a Spring bean. When a bean is instantiated, it may be required to perform some initialization in order to be in a usable state. Similarly, spring context may necessitate cleanup when the bean is no longer required and the container is discarded.
The spring bean factory is in charge of managing the life cycle of beans produced in spring containers. The life cycle of beans consists of call back methods that can be categorized broadly into two groups:
Spring framework offers the following four options for controlling bean life cycle events: