21

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);
    }
}
Abhishek
  • 681
  • 1
  • 6
  • 25

3 Answers3

29

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);
}
Vasu
  • 21,832
  • 11
  • 51
  • 67
  • So, if this typecasting works, I can do this in any class where I can get my hands on `ApplicationContext`. Is it so? – Abhishek Mar 27 '17 at 16:59
  • 1
    But keep `ApplicationContext` only in `SpringUtil`, because it is not a good idea to expose `ApplicationContext` at too many places – Vasu Mar 27 '17 at 17:03
2

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());
Peter Gyschuk
  • 919
  • 8
  • 12
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);
Sergey Nemchinov
  • 1,348
  • 15
  • 21