Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda