Spring Bean Overview

Tram Ho

I. What is Spring Bean?

1. Definition:

Bean is an important part of Spring Boot. To learn more about Spring, it is also important to learn Bean. According to the definition in the Spring Framework documentation:

2. Inversion of Control (IoC)

In simple terms, IoC is an object that initializes dependencies without having to initialize them. These objects delegate the construction and initialization of dependencies to the Ioc Containers.

IoC Container: is where objects are instantiated, configured and embedded in dependencies and managed its lifecycle. It uses Dependency Injection(DI) to manage the components that make up the application. And get the data information from config(XML) files or annotations.

For example, a Person class has an Animal property: public class Person { private Animal animal;

Assume that Animal has 2 properties name and age . When creating a Person, do the following:

The initialization but above is not wrong. But imagine if the Animal class changes (changes properties, adds new properties,…) then the Person class will also change. So with the management when there are so many initialization statements, it will be very time consuming and difficult to manage.

Therefore, the feature of Inverion of Controller will be applied at this time.


Now the Person class is injected with an instantiation of the Animal class.

Here there are 2 Beans declared as Animal and Person.

II. Bean annotations are used a lot in Spring

  1. Component : class-level annotation, default represents a bean value with the same name as the class name starting with a lowercase letter. It can specify other names by passing parameters (param) to annotations.
  2. Repository : class-level annotation, representing the Data Access Object (DAO) class.
  3. Service : class-level annotation, which is where the system’s business is handled and can be reused in many places.
  4. Controller : class-level annotation, which represents the Controller class in the MVC model.
  5. Configuration : class-level annotation, which can contain bean declarations via Bean Annotation

III. Bean Scope

  • Singleton : the container only initializes an instance of the bean and returns itself if required.
  • Prototype : every time there is a request, the container will create a new instance and return it.
  • Request : initializes an instance for an HTTP Request
  • Session : initialize an instance for an HTTP Session
  • Application : initializes an instance for a ServletContext lifecycle
  • WebSocket : initialize an instance for a Websocket Session

IV. Bean’s Life Cycle

The bean life cycle includes the following steps:

  • Bean Definition : initialize the bean using Annotation or XML
  • Bean Instantiation : Spring initializes Bean objects just like regular Java object instantiation and injects it into the ApplicationContext
  • Populating Bean properties: Spring scans beans that implement Aware interfaces and sets values ​​to properties such as id, scope and default values ​​as declared by the bean.
  • Pre-Initialization : The postProcessBeforeInitialization() methods start execution and PostConstruct executes immediately after it.
  • AfterPropertiesSet : Spring implements the afterPropertiesSet() methods of beans that implement InitializingBean
  • Custom Initialization : Spring triggers initialization methods with properties defined in initMethod in Bean annotations
  • Post-initialization : Spring’s BeanPostProcessors works for the second time. This time it fires the postProcessAfterInitialization() methods.
  • Ready : Beans have been initialized and injected into dependencies
  • Pre-Destroy : Spring activates PreDestroy annotated methods in this step
  • Destroy : Spring implements the destroy() methods
  • Custom Destruction : we can customize the destruction times with the destroyMethod attribute in the Bean annotation and Spring will run it in the final phase.

 

Share the news now

Source : Viblo