Performance: Why IoC matters
IoC affects application startup time and runtime memory usage by managing object creation and dependencies efficiently.
@Service
public class Service {
private final Repository repo;
public Service(Repository repo) {
this.repo = repo;
}
}
// Spring IoC container injects dependenciespublic class Service {
private Repository repo = new Repository();
// manual new object creation inside classes
}| Pattern | Object Creation | Startup Time | Memory Usage | Verdict |
|---|---|---|---|---|
| Manual new object creation | Multiple redundant objects | Higher due to repeated instantiation | Higher memory due to duplicates | [X] Bad |
| IoC container managed beans | Singleton or scoped objects reused | Lower by centralized creation | Lower by sharing instances | [OK] Good |
@Component
public class Printer {
public void print() { System.out.println("Hello IoC"); }
}
@Component
public class User {
@Autowired
private Printer printer;
public void show() { printer.print(); }
}
// In main method:
User user = context.getBean(User.class);
user.show();@Component
public class Service {
private Repository repo;
public Service() {
repo = new Repository();
}
}Pass the test and this pattern moves unknown