Bean life cycle and related summary

1. Bean life cycle

The life cycle of Bean is as follows:

1. Instantiation (corresponding to the "loading" of the JVM) (converting bytecode into an object in memory, just allocating memory)

2. Set properties (Bean injection and assembly)

3. Bean initialization

    (1) Various notifications

    (2) Pre-work of initialization

    (3) Perform initialization work (use @PostConstruct, use init-method initialization)

4. Using beans

5. Destroy the Bean


2. @PostConstruct and @PostDestroy methods

@PostConstruct is an annotation that comes with Java. Adding this annotation to a method will execute the method when the project starts. It can also be understood as executing the method when the spring container is initialized.

Starting from the Java EE5 specification, two annotations that affect the Servlet life cycle have been added to the Servlet, @PostConstruct and @PreDestroy. These two annotations are used to modify a non-static void () method.
@PostDestroy is mainly used when destroying beans

 

Guess you like

Origin blog.csdn.net/yss233333/article/details/129638775