RU | EN | DE

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:

  1. Through constructor (the best way)
@Component
class UserService {
    private final UserRepository repo;
    @Autowired
    UserService(UserRepository repo) { this.repo = repo; }
}
  1. Through field (anti-pattern, but encountered)
@Component
class UserService {
    @Autowired
    private UserRepository repo;
}
  1. 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

  1. Object creation
  2. Dependency injection
  3. Call to @PostConstruct
  4. Usage
  5. 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

AnnotationWhat it does
@Componentcomponent bean
@Servicebusiness logic
@RepositoryDAO layer (automatic @Transactional)
@ControllerMVC controller
@RestControllercontroller with @ResponseBody
@Configurationconfiguration class
@Beanbean inside @Configuration
@Autowireddependency injection
@Qualifierbean qualification if multiple
@Valueinject 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=dev

Definition 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.