Spring Getting Started – Full Revision (In Detail)
1. Introduction to Spring (4:19)
What is Spring?
Spring is a Java framework for building robust enterprise applications. It provides:
• Loose coupling via Dependency Injection
• Separation of concerns via well-defined layers
• Easy testability and maintainability
Why use Spring?
• Reduces boilerplate code
• Makes complex enterprise Java development easier
• Supports features like AOP, Transactions, Security, Data Access
Key Modules:
• Spring Core (IoC, DI)
• Spring MVC (Web layer)
• Spring Boot (Rapid development)
• Spring Data JPA, Spring Security, Spring Cloud
2. Spring Docs (1:32)
Useful links:
• spring.io
• Spring Boot Docs
These include:
• Guides (e.g., Building a REST API with Spring)
• API references
• Starters, dependencies, and compatibility info
3. Prerequisites (2:07)
You should know:
• Java Basics: Classes, interfaces, constructors, exceptions
• OOP Concepts: Inheritance, polymorphism, abstraction
• Maven/Gradle: For managing dependencies
• SQL Basics: SELECT, INSERT, UPDATE, DELETE
1
• Optional: HTML/CSS/JS (for full-stack projects)
4. IDE for Spring (7:23)
Recommended IDEs:
• IntelliJ IDEA (Community or Ultimate) – IntelliSense + Spring Boot Integration
• Spring Tool Suite (STS) – Based on Eclipse, preloaded with Spring support
Spring Initializr:
• Used to generate Spring Boot project boilerplate
• Available via start.spring.io or inside IDE
Example:
• Group: com.pranav
• Artifact: myapp
• Dependencies: Spring Web, Spring Boot DevTools, Spring Data JPA, MySQL
5. IoC and DI (4:14)
IoC (Inversion of Control):
• Framework controls object creation, not the programmer
• Promotes loose coupling
DI (Dependency Injection):
• Objects get their dependencies from the container (Spring), not via new
Example:
@Component
public class Laptop {
public void compile() {
System.out.println(”Compiling␣code...”);
}
}
@Component
public class Developer {
@Autowired
Laptop laptop;
public void work() {
laptop.compile();
}
}
Spring manages both beans and injects Laptop into Developer.
6. Spring vs Spring Boot (2:14)
Feature Spring Framework Spring Boot
2
Configuration Manual (XML or Java) Auto-configuration
Server setup External (Tomcat/Jetty) Embedded (no WAR needed)
Boilerplate More code Less code, opinionated defaults
Dev productivity Slower Very fast development
Dependency Manage- Manual Built-in starter dependencies
ment
7. First Spring Boot App (7:56)
Steps:
1. Go to start.spring.io
2. Choose:
• Project: Maven
• Language: Java
• Dependencies: Spring Web
3. Import into IntelliJ or STS
4. Run the app (runs on port 8080 by default)
Sample Code:
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
@RestController
public class HelloController {
@GetMapping(”/”)
public String hello() {
return ”Hello␣Spring␣Boot!”;
}
}
8. DI using Spring Boot (9:06)
Spring Boot uses annotation-based DI.
3 Types:
• Constructor Injection (Best practice)
• Field Injection
• Setter Injection
Example:
@Component
public class Engine {
public void start() {
System.out.println(”Engine␣started!”);
}
}
@Component
3
public class Car {
private final Engine engine;
@Autowired
public Car(Engine engine) {
this.engine = engine;
}
public void drive() {
engine.start();
}
}
Spring Boot injects Engine automatically.
9. Autowiring in Spring Boot (8–9 min)
What is @Autowired?
• Used to tell Spring to automatically inject the dependency
By default, Spring does:
• Type-based injection
• If multiple beans exist, use @Qualifier
Example with Qualifier:
@Component
@Qualifier(”petrol”)
public class PetrolEngine implements Engine {}
@Component
@Qualifier(”diesel”)
public class DieselEngine implements Engine {}
@Component
public class Car {
@Autowired
@Qualifier(”diesel”)
Engine engine;
}
If not specified, Spring will throw NoUniqueBeanDefinitionException.
Summary Table
Topic Purpose Key Annotation/Concept
IoC & DI Loose coupling @Autowired, @Component
Boot App Entry point @SpringBootApplication
REST API Controller @RestController,
@GetMapping
Autowiring Dependency injection @Autowired, @Qualifier