Performance: IoC container mental model
This concept affects application startup time and memory usage by managing object creation and dependencies.
@Service public class Service { private final Repository repo; public Service(Repository repo) { this.repo = repo; } } // Repository is managed by Spring IoC container and injected automatically
public class Service { private Repository repo = new Repository(); // direct instantiation inside the class }
| Pattern | Object Creation | Dependency Management | Startup Impact | Verdict |
|---|---|---|---|---|
| Direct instantiation in classes | Multiple instances per use | Manual, tight coupling | Higher startup time and memory | [X] Bad |
| IoC container with singleton beans | Single shared instance | Automatic, loose coupling | Lower startup time and memory | [OK] Good |
@Component
public class Printer {
public void print() { System.out.println("Hello from Printer"); }
}
@Component
public class Runner {
private final Printer printer;
@Autowired
public Runner(Printer printer) { this.printer = printer; }
@PostConstruct
public void run() { printer.print(); }
}@Component
public class ServiceA {
@Autowired
private ServiceB serviceB;
}
@Component
public class ServiceB {
@Autowired
private ServiceA serviceA;
}Pass the test and this pattern moves unknown