Reputation: 717
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);
}
}
Upvotes: 23
Views: 32178
Reputation: 1616
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);
Upvotes: 1
Reputation: 1029
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());
Upvotes: 4
Reputation: 22422
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);
}
Upvotes: 32