Sobes.tech
Junior — Middle

Kuidas toimub konteksti haldamine Spring Boot rakendustes?

sobes.tech AI

Vastus AI-lt

In Spring Boot, application context management is performed via the Spring ApplicationContext — a container that manages the lifecycle of beans and their dependencies.

Main methods of context management:

  • Automatic component scanning (@ComponentScan) — Spring automatically finds and registers beans.
  • Configuration via @Configuration and @Bean — explicit declaration of beans and their dependencies.
  • Profiles (@Profile) — enable activation of different configurations depending on the environment.
  • Using ApplicationContext to programmatically obtain beans — for example, through injection 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();
}