如何在 Spring WebApplicationContext 中在运行时添加 bean 实例? [英] How to add bean instance at runtime in spring WebApplicationContext?
问题描述
所以,标题非常简单.我有一个处理程序类 DynamicBeanHandler
,它实现了 spring 提供的 BeanDefinitionRegistryPostProcessor
接口.在这个类中,我添加了多个 SCOPE_SINGLETON
bean,它们的 bean 类设置为 MyDynamicBean
,如下所示-
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);
方法 getMutableProperties()
返回一个 MutablePropertyValues
的对象.
The method getMutableProperties()
returns an object of MutablePropertyValues
.
稍后,我执行 SpringUtil.getBean(dynamicBeanId)
以获取所需的 MyDynamicBean
实例,其中 SpringUtil
类实现了 ApplicationContextAware代码>.所有这些都很好用.当我想删除这些实例之一并稍后在我没有注册表实例的地方添加一个新实例时,问题就出现了.任何人都可以帮我找到一种方法吗?
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?
以下是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);
}
}
推荐答案
你可以利用BeanDefinitionRegistry
(看此处 用于 API)以动态删除或注册 bean.
You can make use of BeanDefinitionRegistry
(look here for API) to remove or register the beans dynamically.
因此,在您的 SpringUtil
类中,您可以添加以下方法以使用 removeBeanDefinition()
删除现有的 bean 定义,然后使用 removeBeanDefinition()
添加新的 bean 定义代码>registerBeanDefinition().
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);
}
这篇关于如何在 Spring WebApplicationContext 中在运行时添加 bean 实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!