Basics of Spring Beans

What is the IoC container?

  • IoC, Inversion of Control is the major component of the Spring framework.
  • It manages Spring configured beans.
  • The inversion of the control is also known as the dependency injection.
  • It is all about objects defining their dependencies and other objects using them in the application.
  • @Component, @Service, @Controller and @Repository are automatically registered as Spring beans.

Role of IoC

  • Instantiating beans
  • Wiring beans together
  • Configuring beans
  • Managing bean life-cycle

Types of IoC

In the Spring framework, it has two IoC containers.
  • BeanFactory
    • Responsible for Bean instantiating and wiring
    • Uses LAZY loading
  • ApplicationContext
    • Responsible for Bean instantiating and wiring
    • Automatic BeanPostProcessor registration
    • Automatic BeanFactoryPostProcessor registration
    • Convenient MessageSource access
    • ApplicationEvent publication
    • Uses EAGER loading

How to supply metadata to the Spring IOC container

  • XML based
  • Annotation-based
  • Java-based
This should be described in a separate post. Nowadays usually we are mostly using an annotation-based method.


Spring bean life-cycle

Bean definition

  • Spring beans will be defined using annotations and XML configurations.

Bean creation and instantiation

  • Once a bean is created it will be loaded into the ApplicationContext and JVM memory.

Population bean properties

  • Spring container will create bean id, scope, and default values based on the bean definition.

Post initialization

  • Spring provides aware interfaces to access bean metadata details and callback methods.
  • Aware interfaces 
    • ApplicationContextAware
    • BeanClassLoaderAware
    • BeanNameAware
    • MessageSourceAware
  • Initialization callbacks
    • @PostConstruct
    • InitializingBean.afterPropertySet()

Ready to serve

  • Now the bean is ready to serve.

Pre destroy

  • Spring has some custom logic and clean-ups before destroying the bean.
  • Destruction callbacks
    • @PreDestroy
    • DisposableBean.destroy()

Bean destroy

  • The bean will be removed from the JVM


Why do we need to smash the bean life-cycle

  • Assign default values for bean properties.
  • Load application metadata information while creating a bean and destroying a bean.
  • To make sure application dependency and the modules are up and running fine.


Spring bean scopes 

The default scope of a bean is Singleton. We can use @Scope annotation to change the bean scope.

singleton

  • This is the default scope.
  • The same object is returned every time.
  • Better to use for all stateless beans.

prototype

  • A new object is created every time.
  • Better to use for all stateful beans.

request

  • Here it will create a single instance and it will available in the complete life-cycle of an HTTP request.

session

  • Here it will create a single instance and it will available in the complete life-cycle of an HTTP session.

application

  • Here it will create a single instance and it will available in the complete life-cycle of the servlet context.

websocket

  • Here it will create a single instance and it will available in the complete life-cycle of a web socket.






Basics of Spring Beans Basics of Spring Beans Reviewed by Ravi Yasas on 12:23 PM Rating: 5

No comments:

Powered by Blogger.