Close

Spring - Dynamically register beans

[Last Updated: Nov 4, 2025]

We can programmatically add unregistered objects to the Spring container by creating a BeanDefinition instance.

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

void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)

BeanDefinition

org.springframework.core.AttributeAccessorAttributeAccessororg.springframework.beans.BeanMetadataElementBeanMetadataElementorg.springframework.beans.factory.config.BeanDefinitionBeanDefinitionLogicBig

BeanDefinition is an interface, which describes a bean instance. It has setter methods which can be used to programmatically set the Spring specific characteristics to a bean, for example BeanDefinition #setScope(String scope) can be used to set a scope other than default singleton.

GenericBeanDefinition

java.lang.ObjectObjectorg.springframework.core.AttributeAccessorSupportAttributeAccessorSupportorg.springframework.core.AttributeAccessorAttributeAccessorjava.io.SerializableSerializableorg.springframework.beans.BeanMetadataAttributeAccessorBeanMetadataAttributeAccessororg.springframework.beans.BeanMetadataElementBeanMetadataElementorg.springframework.beans.factory.support.AbstractBeanDefinitionAbstractBeanDefinitionorg.springframework.beans.factory.config.BeanDefinitionBeanDefinitionjava.lang.CloneableCloneableorg.springframework.beans.factory.support.GenericBeanDefinitionGenericBeanDefinitionLogicBig

This is the commonly used BeanDefinition implementation.

It allows to specify the bean class, bean characteristics plus constructor argument values and property values.

In the next tutorials we will see how to work with BeanDefinition.

See Also