Câu hỏi phỏng vấn Spring
Câu hỏi

Explain Bean lifecycle in Spring framework

Câu trả lời

The lifecycle of a Spring bean is a core concept in the Spring Framework, which manages the lifecycle of beans in its Inversion of Control (IoC) container. Understanding this lifecycle is crucial for effectively using the Spring Framework in application development. Here’s a detailed explanation of the Spring bean lifecycle:

Bean Instantiation

The lifecycle begins with the instantiation of a bean. This can be done through various methods such as directly in Java code, using a factory method, or through Spring's dependency injection facilities which can instantiate beans based on XML or annotation-based configuration[1][3].

Populating Bean Properties

Once a bean is instantiated, the Spring IoC container then injects the values and bean references into the bean properties. This process is managed through either XML configuration or annotations[3].

Bean Name Awareness and Other Awareness Interfaces

After populating properties, if the bean implements any of Spring’s awareness interfaces like BeanNameAware, BeanFactoryAware, or ApplicationContextAware, the container will call the relevant methods of these interfaces. This allows the bean to be aware of its metadata and the container that it is being managed by[3].

Pre-initialization (BeanPostProcessors)

Before the bean is fully initialized, the Spring container allows BeanPostProcessors to modify the original bean instance. This can involve wrapping the bean with a proxy for aspects such as transactions or security[3][6].

Initialization

The bean is then initialized. If the bean implements the InitializingBean interface, the container calls its afterPropertiesSet() method. Alternatively, if the bean’s configuration defines an init method, that method is called. This step is used to perform any initialization logic that the bean may need before it is ready ...

middle

middle

Gợi ý câu hỏi phỏng vấn

middle

What is Aspect?

junior

Explain @RequestMapping annotation.

senior

What are Aspect, Advice, Pointcut, and JoinPoint in AOP?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào