Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
How to add bean instance at runtime in spring WebApplicationContext?

So, the title is pretty straightforward. I have a handler class DynamicBeanHandler which implements BeanDefinitionRegistryPostProcessor interface provided by spring. In this class I'm adding multiple SCOPE_SINGLETON beans which have bean class set as MyDynamicBean as follows-

GenericBeanDefinition myBeanDefinition = new GenericBeanDefinition();
myBeanDefinition.setBeanClass(MyDynamicBean.class);
myBeanDefinition.setScope(SCOPE_SINGLETON);
myBeanDefinition.setPropertyValues(getMutableProperties(dynamicPropertyPrefix));
registry.registerBeanDefinition(dynamicBeanId, myBeanDefinition);

The method getMutableProperties() returns an object of MutablePropertyValues.

Later on, I do SpringUtil.getBean(dynamicBeanId) to fetch the required MyDynamicBean instance where SpringUtil class implements ApplicationContextAware. All this works great. The problem comes when I want to remove one of these instances and add a new instance later on where I don't have the registry instance. Can anyone please help me find a way to do this?

Following is the code for class SpringUtil-

public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtil.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String beanId) {
        return applicationContext.getBean(beanId);
    }

    public static <T> T getBean(String beanId, Class<T> beanClass) {
        return applicationContext.getBean(beanId, beanClass);
    }
}
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can make use of BeanDefinitionRegistry (look here for API) to remove or register the beans dynamically.

So, in your SpringUtil class, you can add the below method to remove the existing bean definition using removeBeanDefinition() and then add a new bean definition by using registerBeanDefinition().

public void removeExistingAndAddNewBean(String beanId) {

   AutowireCapableBeanFactory factory = 
                   applicationContext.getAutowireCapableBeanFactory();
   BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
   registry.removeBeanDefinition(beanId);

    //create newBeanObj through GenericBeanDefinition

    registry.registerBeanDefinition(beanId, newBeanObj);
}
about 4 years ago · Santiago Trujillo Report

0

You can programmatically create a bean with injected dependencies this way:

AutowireCapableBeanFactory#createBean

Fully create a new bean instance of the given class with the specified autowire strategy. All constants defined in this interface are supported here. Performs full initialization of the bean, including all applicable BeanPostProcessors.

@Autowired
ApplicationContext context;

AutowireCapableBeanFactory factory = context.getAutowireCapableBeanFactory();
MyDynamicBean bean = (MyDynamicBean) factory.createBean(MyDynamicBean.class, AUTOWIRE_CONSTRUCTOR, false);
about 4 years ago · Santiago Trujillo Report

0

Instead of autowiring WebApplicationContext you can do

@Autowired
private GenericWebApplicationContext context;

Then you can do register new bean or remove old one and register new bean

AutowireCapableBeanFactory factory = context.getAutowireCapableBeanFactory();
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
registry.removeBeanDefinition("myBean");
context.registerBean("myBean", MyBean.class, () -> new MyBean());
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!