1. IoC (Inversion of Control)
Idea: Instead of you creating objects manually (new), the Spring container manages their creation and binding.
👉 In typical Java:
class Car {
private Engine engine = new Engine(); // tight coupling
}👉 In Spring (via DI):
@Component
class Engine {}
@Component
class Car {
private final Engine engine;
@Autowired
Car(Engine engine) { this.engine = engine; }
}⚡ Now the Spring container creates Engine and Car.
2. DI (Dependency Injection)
Types of DI:
- Through constructor (the best way)
@Component
class UserService {
private final UserRepository repo;
@Autowired
UserService(UserRepository repo) { this.repo = repo; }
}- Through field (anti-pattern, but encountered)
@Component
class UserService {
@Autowired
private UserRepository repo;
}- Through setter
@Component
class UserService {
private UserRepository repo;
@Autowired
public void setRepo(UserRepository repo) {
this.repo = repo;
}
}3. Beans
Bean = object managed by Spring.
👉 Creating a bean:
@Component
class MyService {}👉 Alternative (Java Config):
@Configuration
class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}4. ApplicationContext
Spring stores all beans in the ApplicationContext (container). Example of starting without Spring Boot:
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(AppConfig.class);
MyService service = ctx.getBean(MyService.class);
service.doWork();In Spring Boot, this happens automatically.
5. Bean Lifecycle
- Object creation
- Dependency injection
- Call to @PostConstruct
- Usage
- Destruction (call to @PreDestroy)
@Component
class MyBean {
@PostConstruct
public void init() {
System.out.println("Bean initialized");
}
@PreDestroy
public void cleanup() {
System.out.println("Bean destroyed");
}
}6. Scope (Bean scopes)
- singleton (default) → one object per application
- prototype → new object on each request
- request (Web) → one object per HTTP request
- session (Web) → one object per session
@Component
@Scope("prototype")
class PrototypeBean {}7. Spring Core Annotations
| Annotation | What it does |
|---|---|
| @Component | component bean |
| @Service | business logic |
| @Repository | DAO layer (automatic @Transactional) |
| @Controller | MVC controller |
| @RestController | controller with @ResponseBody |
| @Configuration | configuration class |
| @Bean | bean inside @Configuration |
| @Autowired | dependency injection |
| @Qualifier | bean qualification if multiple |
| @Value | inject values from application.properties |
8. Configurations (Java, XML, Annotations)
Today, Java Config and annotations are almost always used.
@Configuration
class MyConfig {
@Bean
public Engine engine() {
return new Engine();
}
}9. Profiles (different environments)
application.properties:
spring.profiles.active=devDefinition of a bean for a specific profile:
@Component
@Profile("dev")
class DevDatabaseConfig {}10. Best Practices
- ✅ Use constructor injection → easier to test, fewer null problems.
- ✅ Make classes @Service → for business logic, @Repository → for database operations.
- ✅ Do not use @Autowired on fields (only on the constructor).
- ✅ Control scope → singleton is best by default.
- ✅ Use @Configuration and @Bean for complex dependencies.