Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Monday, August 10, 2020

Spring Framework BeanDefinitionRegistryPostProcessor Example

1. Introduction


In this tutorial, You'll learn about BeanDefinitionRegistryPostProcessor interface in Spring and Spring Boot.

Spring Framework API - BeanDefinitionRegistryPostProcessor is used to register the bean dynamically with application context at runtime. This is most useful in the realtime when working with client libraries those are changed frequently. By using this bean, you can reduce the deployments of the application but you should use this with care. Otherwise, It will override the existing bean configuration.

Spring BeanDefinitionRegistryPostProcessor interface has one abstract method "void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException".

This is mainly useful if you have any third-party libraries which are not spring beans. So, these beans can be registered dynamically at runtime based on your need.

BeanDefinitionRegistryPostProcessor is also one of the interfaces to register the beans at runtime without using @Bean or @Component annotations in the program.

Spring Framework BeanDefinitionRegistryPostProcessor Example


From API:

Extension to the standard BeanFactoryPostProcessor SPI, allowing for the registration of further bean definitions before regular BeanFactoryPostProcessor detection kicks in. In particular, BeanDefinitionRegistryPostProcessor may register further bean definitions which in turn define BeanFactoryPostProcessor instances.

Spring Boot BeanDefinitionRegistryPostProcessor


So, Any classes that implement the BeanDefinitionRegistryPostProcessor interface will be executed before the start BeanFactoryPostProcessor registration.

Monday, March 23, 2020

Spring - Dynamically register beans in 4 ways At Run-Time

1. Overview

In this tutorial, We will learn about "dynamically register bean with spring" or "dynamically add the bean to spring-context" (at run time).
This can be done without restarting the application at runtime when Loading and Removing bean in Spring Application.

If the client code needs to register objects which are not managed by Spring container, then we will need to work with an instance of BeanDefinition.

A Spring application can register a BeanDefinition by using the following method of BeanDefinitionRegistry:

void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)

Here, We have used the following dependencies.
  •     Spring Context 4.3.4.RELEASE: Spring Context.
  •     JDK 1.8
  •     Maven 3.3.9
Spring - Dynamically register beans (At Run-Time)