Spring Framework
What is a Framework?
A framework is a reusable, pre-built software platform designed to simplify application development. Instead of building everything from scratch, developers can use a framework to speed up the development process by focusing on business logic.
Examples:
Frameworks help:
A framework is a reusable, pre-built software platform designed to simplify application development. Instead of building everything from scratch, developers can use a framework to speed up the development process by focusing on business logic.
Examples:
- Spring
- Hibernate
Frameworks help:
- Reduce boilerplate code
- Increase productivity
- Simplify complex coding tasks
What is the Spring Framework?
The Spring Framework is a Java-based framework used to build enterprise-level applications like e-commerce platforms (e.g., Amazon, Flipkart, Naaptol).
Key features:
The Spring Framework is a Java-based framework used to build enterprise-level applications like e-commerce platforms (e.g., Amazon, Flipkart, Naaptol).
Key features:
- Lightweight
- Modular
- Supports Dependency Injection
- Encourages loose coupling
- Enables better maintainability and testability
Core Concepts in Spring Framework
Inversion of Control (IoC)
Definition:
IoC is a principle where the control of object creation and management is transferred from the application to the Spring container.
Instead of manually creating objects using new, Spring manages and injects them as needed.
Common Implementations of ApplicationContext
XML Configuration (beans.xml):
Main Class:
Inversion of Control (IoC)
Definition:
IoC is a principle where the control of object creation and management is transferred from the application to the Spring container.
Instead of manually creating objects using new, Spring manages and injects them as needed.
IoC is implemented in Spring using Dependency Injection (DI).
Dependency Injection (DI)
Definition:
Dependency Injection is a design pattern used to inject the dependency (object) of one class into another.
Spring supports two main types of DI:
Definition:
Dependency Injection is a design pattern used to inject the dependency (object) of one class into another.
Spring supports two main types of DI:
- Constructor Injection
- Setter/Property Injection
Design Pattern
Definition:
A design pattern is a common solution to a recurring problem in software design.
Examples: Used in Spring
Definition:
A design pattern is a common solution to a recurring problem in software design.
Examples: Used in Spring
- Singleton Design Pattern
- Dependency Injection
Spring Container (IoC Container)
The Spring Container is the core of the Spring Framework. It is responsible for:
The Spring Container is the core of the Spring Framework. It is responsible for:
- Creating objects (beans)
- Managing their lifecycle
- Injecting dependencies
Key Point
ApplicationContext
ApplicationContext is the most commonly used interface in Spring that represents the Spring IoC container.
Common Implementations of ApplicationContext
Class Name | Description |
---|---|
ClassPathXmlApplicationContext | Loads XML configuration from the classpath |
FileSystemXmlApplicationContext | Loads XML from the file system |
WebXmlApplicationContext | Used in web-based Spring apps |
AnnotationConfigApplicationContext | Loads configuration from annotated classes |
Rules for Creating a Java Bean Class in Spring
To create a valid Spring bean:
To create a valid Spring bean:
- Must be a public non-abstract class
- Use private data members
- Provide public getter and setter methods
Steps to Achieve Dependency Injection (Using Setter Injection)
Example: Simple Setter Injection
Bean Class:
- Create a Java project with required packages
- Download and add Spring JAR files to the project’s build path
- Create a Java Bean class (with getters and setters)
- Create an XML configuration file (e.g., beans.xml)
- Load the config using ApplicationContext and get the bean
- Use the injected bean in your business logic
Example: Simple Setter Injection
Bean Class:
public class Student { private String name;
public void setName(String name) { this.name = name; }
public void displayInfo() { System.out.println("Hello: " + name); }}
XML Configuration (beans.xml):
<beans> <bean id="student" class="com.example.Student"> <property name="name" value="John Doe" /> </bean></beans>
Main Class:
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");Student student = (Student) context.getBean("student");student.displayInfo();