Dynamic Bean Registration in Spring¶
Dynamic Bean Registration is the process of programmatically adding or removing bean definitions to and from the Spring IoC container during runtime, rather than solely relying on static declarations (such as @Component or XML configuration)^[600-developer__java__spring__spring-note.md].
This capability allows developers to modify the application context and its managed beans on-the-fly based on runtime conditions, configuration, or specific logic^[600-developer__java__spring__spring-note.md].
Implementation via BeanFactory¶
The most direct method to achieve dynamic registration involves interacting with the DefaultListableBeanFactory^[600-developer__java__spring__spring-note.md].
To perform these operations, the underlying BeanFactory must be retrieved from the ApplicationContext^[600-developer__java__spring__spring-note.md].
Registration Process¶
The core mechanism for registering a bean programmatically involves the following steps^[600-developer__java__spring__spring-note.md]:
- Obtain the BeanFactory: Cast the
AutowireCapableBeanFactoryretrieved from theApplicationContextto aDefaultListableBeanFactory^[600-developer__java__spring__spring-note.md]. - Create Bean Definition: Use
BeanDefinitionBuilder.genericBeanDefinition(Class)to create a definition builder for the target class^[600-developer__java__spring__spring-note.md]. - Configure Properties:
- Use
addPropertyValue(key, value)to set primitive or direct object values^[600-developer__java__spring__spring-note.md]. - Use
addPropertyReference(key, beanName)to inject references to other existing beans in the context^[600-developer__java__spring__spring-note.md].
- Use
- Register: Invoke
registerBeanDefinition(String beanName, BeanDefinition beanDefinition)on the factory to register the bean^[600-developer__java__spring__spring-note.md].
Naming Convention¶
A utility method can be used to generate a bean name based on the class's simple name (converted to CamelCase) and a specific reference string to ensure uniqueness^[600-developer__java__spring__spring-note.md].
Deletion¶
Beans can be dynamically removed from the context using the removeBeanDefinition(String beanName) method on the DefaultListableBeanFactory^[600-developer__java__spring__spring-note.md].
Implementation via BeanDefinitionRegistryPostProcessor¶
For registering beans during the container startup phase—before the context is fully refreshed—Spring provides the BeanDefinitionRegistryPostProcessor interface^[600-developer__java__spring__spring-note.md].
- Interface:
BeanDefinitionRegistryPostProcessor - Method:
postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) - Usage: This method is invoked during the container's initialization lifecycle. It allows for bulk registration of beans (e.g., in a loop) before other beans are instantiated^[600-developer__java__spring__spring-note.md].
This approach is often preferred when the set of beans to be registered is known at startup time but is dynamic based on configuration or environment, rather than being triggered by a runtime event after the application has started^[600-developer__java__spring__spring-note.md].
Related Concepts¶
- [[ApplicationContext]]
- [[Spring]]
- [[FactoryBean]]
- [[ImportBeanDefinitionRegistrar]]
Sources¶
600-developer__java__spring__spring-note.md