Performance: Bean concept in Spring
This concept affects application startup time and memory usage by managing object creation and lifecycle.
@Component public class Service { private final Repository repo; public Service(Repository repo) { this.repo = repo; } } @Component public class Repository {}
public class Service {
private final Repository repo = new Repository();
// manual new object creation without Spring
}| Pattern | Bean Creation | Memory Usage | Startup Time | Verdict |
|---|---|---|---|---|
| Manual object creation | Multiple unmanaged instances | High due to duplicates | Longer due to no container optimization | [X] Bad |
| Spring-managed singleton beans | Single shared instance | Optimized minimal usage | Faster due to container control | [OK] Good |
| Eager loading many beans | All beans created at startup | Moderate to high | Slower startup | [!] OK |
| Lazy initialization beans | Created on demand | Lower initial memory | Faster startup | [OK] Good |
Bean in the Spring framework?@Component
public class MyBean {
public String greet() { return "Hello Spring"; }
}
@Autowired
MyBean myBean;
System.out.println(myBean.greet());@Component
public class MyService {
@Autowired
private MyRepository repo;
public void doWork() {
repo.save();
}
}MyRepository is not annotated as a bean.Pass the test and this pattern moves unknown