将xml定义中的其他bean注册到已初始化的应用程序上下文中 [英] Register additional beans from xml definition into application context that is already initialized

查看:58
本文介绍了将xml定义中的其他bean注册到已初始化的应用程序上下文中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经初始化了一个应用程序上下文,此外,我还需要从xml定义中加载另一个bean.

I've got an application context already initialized and I need to load another beans from xml definition into it in addition.

我可以执行applicationContext.getAutowireCapableBeanFactory(),但这只是用于自动装配某些对象的属性.

I can do applicationContext.getAutowireCapableBeanFactory() but it is just for autowiring properties of some Object.

我找不到通过XmlBeanDefinitionReader和ContextLoader进行操作的方法,因为如您所见,只有公共方法是loadContext(String... locations),它总是创建一个新的上下文.

I can't find how to do that via XmlBeanDefinitionReader and ContextLoader, because as you can see, only public method is loadContext(String... locations) and it always creates a new context.

public final ConfigurableApplicationContext loadContext(String... locations) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug("Loading ApplicationContext for locations [" +
                StringUtils.arrayToCommaDelimitedString(locations) + "].");
    }
    GenericApplicationContext context = new GenericApplicationContext();
    prepareContext(context);
    customizeBeanFactory(context.getDefaultListableBeanFactory());
    createBeanDefinitionReader(context).loadBeanDefinitions(locations);
    AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
    customizeContext(context);
    context.refresh();
    context.registerShutdownHook();
    return context;
}

推荐答案

如果我正确理解这一点,则希望将Bean从xml位置加载到已经存在的应用程序上下文中.你只是像这样afaik:

If I understand that correctly, you want to load beans from xml locations to an already existing application context. You just go like this afaik :

  ApplicationContext context;

  BeanDefinitionReader beanDefReader = new XmlBeanDefinitionReader(context) ;
  beanDefReader.loadBeanDefinitions(locations);
  context.refresh();

这篇关于将xml定义中的其他bean注册到已初始化的应用程序上下文中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆