Junior — Middle
How is context management carried out in Spring Boot applications?
sobes.tech AI
Answer from AI
In Spring Boot, application context management is carried out through the Spring ApplicationContext — a container that manages the lifecycle of beans and their dependencies.
Main ways to manage the context:
- Automatic component scanning (@ComponentScan) — Spring automatically finds and registers beans.
- Configuration via @Configuration and @Bean — explicit declaration of beans and their dependencies.
- Profiles (@Profile) — allow activating different configurations depending on the environment.
- Using ApplicationContext to programmatically obtain beans — for example, through injecting ApplicationContext and calling getBean().
Example of obtaining a bean from the context:
@Autowired
private ApplicationContext context;
public void example() {
MyService service = context.getBean(MyService.class);
service.doWork();
}