Talking about Spring's Bean Life Cycle and Scope

I have been learning spring for a while. Some people say that the development of Spring is the development of Java, and Spring is half of Java, and AOP thinking is an extension and extension of OOP thinking. So today, let's talk about the life cycle of spring and its scope.

1. The life cycle of Spring's Bean

When it comes to the superiority of the Java programming language, there are nothing more than two points:"Write once, run everywhere"And its uniqueGarbage collection mechanism. Spring beans are the same as Java objects. It is nothing more than a different container. One is in the JVM heap and the other is in the IOC container. All are to prevent memory overflow, and also have their own similar life cycles, init, work, destroy, etc.

 借鉴==《Spring In Action》==所讲的生命周期11步:
 1.实例化;
 2.填充属性;
 3.调用BeanNameAware接口的setBeanName()方法;
 4.调用BeanFactoryAware接口的setBeanFactory()方法;
 5.调用ApplicationContextAware接口的setAppplicationContext()方法;
 6.调用BeanPostProcessor的预初实化方法;
 7.调用InitializingBean的setPropertiesSet()方法;
 8.调用自定义的初始化方法
 9.调用BeanProcessor的初始化后方法;
 10.调用Disposable的destroy()方法
 11.调用自定义的销毁方法。

The seemingly complex 11-step life cycle is actually a life cycle from null to initialization, assembly, work, and destruction to null.

2. The scope of Spring's Bean

The scope of spring is roughly divided into five types

singleton

Singleton in English means singleton. A friend who has known 23 design patterns is very familiar with the singleton pattern because it is the most classic design pattern.

There are 8 kinds of singleton modes , the typical ones are the lazy man and the hungry man, which are two object creation ideas of instant loading and lazy loading respectively. The advantage of the singleton mode is to avoid creating large objects repeatedly, causing memory overflow. Of course, spring's singleton scope is also a reference to the idea of ​​singleton. The default scope of spring's bean is green and environmentally friendly in the code world Xing Daorong's opinion.

prototype

The English translation of prototype is the meaning of prototype. Prototype model of 23 design patterns.

The core idea of ​​the prototype model is deep clone and shallow clone. The difference between dark and light cloning is literally complicated. However, it is easy for students who have understood Java's garbage collection mechanism.

Java's garbage collection algorithms are mainly divided into two categories:Reference counting algorithmwithRoot search algorithm.
The reference technology algorithm is to add a reference calculator to the object, add one for each additional reference, and subtract one when the reference becomes invalid. When the counter is 0, the object is not used, and the garbage collector can collect the object. The root search algorithm judges whether an object can be connected to "gc root" through a node. If not, it means that the object is unreachable, and the garbage collector can reclaim the memory space of the object. In fact, the first algorithm of the two spam search algorithms only depends on whether it is used or not, and the second depends on whether it is referenced by other objects.

The same is true for deep and shallow clones. Shallow clones clone a single object, and deep clones not only clone a single object, but also the reference relationship of this object. The prototype mode will create a separate instance object for each getBean request. Considering the characteristics of singleton and prototype patterns, unless in a special environment, frequent creation of instance objects will cause performance waste.

The above two are common scopes of spring beans, and the next three are life cycles that only appear in the web environment.

session

The English translation of session means the main course. In the web environment, the session is the server-side session, and the life of the bean is in the wholesession domainin.

request

Spring will create an instance for each HTTP request.

globalSession

The fifth type of scope is the global session scope that is not introduced in "Spring In Action" and many documents . It may be because of its limited scope of use, because only on the webportlet containerOnly used in.

Dao Rong's ability is limited, so let's talk about it. I hope that some of my feelings can be quoted to get more different opinions from everyone. Many knowledge points of the article are borrowed from "Spring In Action" and some articles of Geek Academy.

Guess you like

Origin blog.csdn.net/qq_39414348/article/details/103615673