DispatcherServlet
The DispatcherServlet is the front controller in the Spring MVC framework. It acts as a central dispatcher for HTTP requests, directing them to the appropriate controller, service, or view.
Spring MVC (Model-View-Controller)
Definition: A web framework that provides a pattern for developing web applications. It separates the application into three layers:
- Model: Represents the data and business logic.
- View: Displays the user interface (UI).
- Controller: Handles user requests, updates the model, and returns the view.
DispatcherServlet: The core component of Spring MVC that routes HTTP requests to the appropriate handler methods.
Dependency Injection (DI)
Definition: A core concept in Spring that allows objects to be created and managed by the Spring container, rather than manually creating them. DI helps to reduce tight coupling between classes and makes the code more modular and testable.
Types: Constructor Injection, Setter Injection, and Field Injection.
Inversion of Control (IoC)
Definition: A design principle in which the control of object creation and lifecycle is shifted from the application code to the Spring framework. It works hand-in-hand with Dependency Injection.
Spring IoC Container: Manages the beans (objects) in a Spring application and their lifecycle.
Allgemeinwissen – Java
Java follows the Object-Oriented Programming (OOP) paradigm, which consists of four main principles:
Concept | Definition | Achieved By |
Encapsulation | Hiding data and providing access through methods | Private fields, public getters/setters |
Abstraction | Hiding implementation details, showing only functionality | Abstract classes, Interfaces |
Inheritance | Child class inherits properties from parent class | extends keyword |
Polymorphism | One interface, multiple implementations | Method Overloading, Method Overriding |
Encapsulation
Encapsulation is the concept of hiding data and restricting direct access to it. It allows access only through getter and setter methods.
- Data hiding (using private variables)
- Controlled access (via public methods)
- Improves security and maintainability
class BankAccount {
private double balance; // Private variable (hidden data)
public void setBalance(double balance) { // Setter method
this.balance = balance;
}
public double getBalance() { // Getter method
return balance;
}
}
public class Main {
public static void main(String[] args) {
BankAccount acc = new BankAccount();
acc.setBalance(1000.50); // Setting balance
System.out.println(acc.getBalance()); // Accessing balance
}
}
Why use encapsulation? → It protects the data from unauthorized access and modification.
Abstraction
Abstraction is the concept of hiding implementation details and exposing only essential features.
It is achieved using abstract classes (abstract keyword) and interfaces.
Hides complex implementation
Focuses on functionality
Reduces code complexity
abstract class Vehicle {
abstract void start(); // Abstract method (no implementation)
}
class Car extends Vehicle {
void start() { // Implementation provided
System.out.println("Car starts with a key.");
}
}
public class Main {
public static void main(String[] args) {
Vehicle myCar = new Car();
myCar.start(); // Output: Car starts with a key.
}
}
Why use abstraction? → It provides a blueprint and enforces structure while allowing flexibility in implementation.
Inheritance
Inheritance allows a class to acquire the properties and methods of another class.
The class that inherits is called a child/subclass.
The class being inherited is called a parent/superclass.
Promotes code reusability
Establishes a parent-child relationship
Reduces redundancy
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal { // Dog inherits from Animal
void bark() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.makeSound(); // Inherited method
d.bark(); // Child class method
}
}
Why use inheritance? → It helps in creating hierarchical relationships and avoids duplicate code.
Polymorphism
Polymorphism means „many forms“ and allows a single method to behave differently based on the object calling it.
Types of Polymorphism:
Compile-time Polymorphism (Method Overloading): Same method name, different parameters
Runtime Polymorphism (Method Overriding): Same method name, same parameters, different behavior
Example (Method Overloading):
class MathUtils {
int add(int a, int b) { // Two parameters
return a + b;
}
int add(int a, int b, int c) { // Three parameters
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
MathUtils obj = new MathUtils();
System.out.println(obj.add(5, 10)); // Calls first method
System.out.println(obj.add(5, 10, 15)); // Calls second method
}
}
Example (Method Overriding – Runtime Polymorphism):
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void makeSound() { // Overriding the method
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Dog(); // Upcasting
myAnimal.makeSound(); // Calls overridden method in Dog
}
}
Why use polymorphism? → It improves flexibility and scalability in the code.