Last Updated : 04 Aug, 2025
The Spring Framework is a lightweight Java framework widely used for building scalable, maintainable enterprise applications. It offers a comprehensive programming and configuration model for Java-based development.
Benefits of Using Spring FrameworkDependency Injection is a design pattern used in software development to implement Inversion of Control. It allows a class to receive its dependencies from an external source rather than creating them within the class. This reduces the dependency between classes and makes the system more maintainable.
Types of Dependency Injection
Constructor Injection: In constructor injection, the dependent object is provided to the class via its constructor. The dependencies are passed when an instance of the class is created.
Example: This example demonstrates, the Car class depends on the Engine class and the dependency is provided via the constructor.
Java
// Constructor Injection
public class Car {
private Engine engine;
public Car(Engine engine) {
this.engine = engine;
}
}
Setter Injection: In setter injection, the dependent object is provided to the class via a setter method after the class is instantiated. This allows you to change the dependencies dynamically.
Example: This example, demonstrates the Car class receives the Engine class through a setter method, which is called after the Car object is created.
Java
// Setter Injection
public class Car {
private Engine engine;
public void setEngine(Engine engine) {
this.engine = engine;
}
}
Field Injection: In field injection, the dependent object is directly injected into the class through its fields. It is done using framework like Spring(via annotations). The Dependency Injection automatically injects the dependency without requiring explicit constructor or setter methods.
Example: This example, demonstrates the Engine class is injected directly into the Car class through its field, using annotations or DI frameworks.
Java
// Field Injection
public class Car {
@Autowired
private Engine engine;
}
2. Inversion of Control (IOC) Container
Inversion of Control (IoC) is a design principle used in object-oriented programming where the control of object creation and dependency management is transferred from the application code to an external framework or container. This reduces the complexity of managing dependencies manually and allows for more modular and flexible code.
In Spring framework there are mainly two types of IOC Container which are listed below:
BeanFactory: BeanFactory is the simplest container and is used to create and manage beans. It is a basic container that initializes beans lazily (i.e., only when they are needed). It is typically used for lightweight applications where the overhead of ApplicationContext is not required.
Example:
Resource resource = new ClassPathResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);
MyBean obj = (MyBean) factory.getBean("myBean");
Explanation:
Note: This will create a Car bean inside the IoC container, which will be initialized when requested.
Application Context: ApplicationContext is an advanced container that extends BeanFactory and provides additional features like internationalization support, event propagation and AOP (Aspect-Oriented Programming) support. The ApplicationContext is preferred in most Spring applications because of its enhanced features.
Example:
3. Spring AnnotationApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
MyBean obj = (MyBean) context.getBean("myBean");
Spring Annotations are metadata used by the Spring Framework to define configuration, dependencies and behavior directly in Java code. They allow Spring to automatically detect, create and manage beans at runtime using component scanning and reflection.
Lets discuss some common type of annotation:
These annotations are key for managing the Spring IoC (Inversion of Control) container and defining dependencies in our application.
Architecture of Spring FrameworkThe Spring framework consists of seven modules which are shown in the above Figure. These modules are Spring Core, Spring AOP, Spring Web MVC, Spring DAO, Spring ORM, Spring context and Spring Web flow. These modules provide different platforms to develop different enterprise applications; for example, you can use Spring Web MVC module for developing MVC-based applications.
Spring Framework ModulesFeatures
Spring
Java EE
Hibernate
Types
It is a lightweight, modular framework for enterprise applications.
It is a heavyweight platform providing a comprehensive set of services.
It is a framework focuses on ORM(Object Relational Mapping)
Modularity
Highly modular means we can use only the needed components.
It comes with a large predefine set of APIs
It entirely focuses on database interaction
Focus Areas
Focuses on Dependency Injection, AOP and MVC for scalability.
Primarily focuses on enterprise-level services like messaging and transactions.
Simplifies database operations, mapping objects to tables.
Transaction Management
Flexible, supports both declarative and programmatic transactions.
Centralized, relies on JTA for managing transactions.
Manages database transactions, but no broader transaction services.
Evolution of Spring FrameworkThe framework was first released under the Apache 2.0 license in June 2003. After that there has been a significant major revision, such as Spring 2.0 provided XML namespaces and AspectJ support, Spring 2.5 provide annotation-driven configuration, Spring 3.0 provided a Java-based @Configuration model. As of 2025, the latest release is Spring Framework 6, which supports Java 17+, Jakarta EE 10 and native compilation using GraalVM. Earlier major versions include Spring 5 (supporting reactive programming and Java 8+). Though you can still use Spring with an older version of java, the minimum requirement is restricted to Java SE 6. Spring 4.0 also supports Java EE 7 technologies, such as java message service (JMS) 2.0, java persistence API (JPA) 2.1, Bean validation 1.1, servlet 3.1 and JCache.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4