How to understand bean objects in the spring framework?

Time:2021-9-8

Definition of bean object?

In the spring framework, the objects of objects created and managed by spring are called bean objects.

Characteristics of bean objects?

In order to manage and apply bean objects more scientifically, spring framework designs relevant features for bean objects, such as lazy loading, scope and life cycle methods.

Lazy load

In the spring framework, by default, all bean objects will be created at startup. However, if some bean objects are not used for a long time, they will be created at startup, which will occupy their memory resources, resulting in a certain waste of resources. At this time, we can delay the creation of objects based on the lazy loading strategy and add @ lazy annotation on the class when designing the class, for example:

package com.cy.pj.common.pool;
@Component
@Lazy
public class ObjectPool {
  public ObjectPool() {
    System.out.println("ObjectPool()");
  }
}

There is a value attribute in the @ lazy annotation, and its default value is true, indicating delayed loading.

Scope

After the spring framework creates an object, it can give the object a scope so that the object can be reused within a certain range. There are two common types: singleton and prototype, among which:

  • Singleton: this scope means that there can only be one bean object instance with the same name and type in the whole spring container. When this instance is created is related to the delayed loading feature configuration of the class. After this instance is created, the life cycle will be managed by the spring framework.
  • Prototype: this scope means that every time an object is obtained from the spring container, a new instance will be created. This instance will be created when necessary, which has nothing to do with the lazy feature. After this instance is created, spring can initialize it, but is not responsible for destroying it.

Set bean scope based on annotation @ scope. Code demonstration:

package com.cy.pj.common.pool;、
@Lazy
@Scope("singlton")
@Component
public class ObjectPool {
  public ObjectPool() {
    System.out.println("ObjectPool()");
  }
}

The scope of the default bean object in spring is singleton. If you want to be prototype, you can use @ scope (“prototype”)

Life cycle approach

Each object in the program has a life cycle, but when not every object type is defined, the life cycle method must be defined. If additional initialization is required after the creation of a bean object, additional release of other resources is also required before destruction. The spring framework can define its life cycle method for the class. If it is annotation, we can implement it with the following two annotations.

  • @Postconstruct annotation is used to describe the initialization method in the bean object life cycle method. This method will be executed after the construction method of the object (initialization after object creation).
  • @Predestroy annotation is used to describe the destruction method in the bean object life cycle method. This method will be executed before the object is destroyed (when the scope is prototype, this method will not be executed).

Code demonstration:

package com.cy.pj.common.pool;、
@Lazy
@Scope("singlton")
@Component
public class ObjectPool {
  public ObjectPool() {
    System.out.println("ObjectPool()");
  }
  @PostConstruct 
  public void init() {
    System.out.println("init()");
  }
  @PreDestroy
  public void close() {
    System.out.println("close()");
  }
}

Generally, pool objects will set some life cycle methods, such as connection pool.

Summary

This section mainly makes a summary analysis of the bean object features in the spring framework. Through this analysis, you can understand how the spring framework scientifically applies bean objects.

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]