簡體   English   中英

將xml定義中的其他bean注冊到已經初始化的應用程序上下文中

[英]Register additional beans from xml definition into application context that is already initialized

我已經初始化了一個應用程序上下文,此外我還需要從xml定義中加載另一個bean。

我可以執行applicationContext.getAutowireCapableBeanFactory(),但這僅用於自動裝配某些Object的屬性。

我找不到如何通過XmlBeanDefinitionReader和ContextLoader做到這一點的方法,因為如您所見,只有公共方法是loadContext(String... locations)並且它始終會創建一個新的上下文。

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;
}

您必須通過將創建的上下文設置為父上下文的子級來“合並”兩個ApplicationContext並刷新父級:

GenericApplicationContext context = new GenericApplicationContext();
context.setParent(parentContext);
parentContext.refresh();

如果我理解正確,則希望將Bean從xml位置加載到現有的應用程序上下文中。 你只是像這樣afaik:

  ApplicationContext context;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM