How can I inject a bean into an ApplicationContext before it loads from a file?

后端 未结 3 1726
星月不相逢
星月不相逢 2021-02-06 01:56

I have a FileSystemXmlApplicationContext and I would like the beans defined in the XML to take as a constructor argument a bean which is not declared in Spring

相关标签:
3条回答
  • 2021-02-06 02:12

    If the existing context needs the bean you wish to inject then things need to be done a little differently. The approaches in other answers don't work for the following reasons

    • Context cannot be refreshed until the missing bean is registered
    • refresh() causes the injected bean to be destroyed.

    This can be worked around by using a "bean factory post processor", this allows code to be run after the context is loaded but before it it refreshed.

    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext();
    applicationContext.setConfigLocation("/org/example/app-context.xml");
    applicationContext.getBeanFactoryPostProcessors().add(new BeanFactoryPostProcessor() {
    
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            beanFactory.registerSingleton("customBeanName", customBean);
        }
    });
    applicationContext.refresh();
    
    0 讨论(0)
  • 2021-02-06 02:14

    As I had trouble solving this with an AnnotationConfigApplicationContext, I found the following alternative:

    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    beanFactory.registerSingleton("customBean", new CustomBean());
    context = new AnnotationConfigApplicationContext(beanFactory);
    context.register(ContextConfiguration.class);
    context.refresh();
    
    0 讨论(0)
  • 2021-02-06 02:29

    How about programmatically creating an empty parent context first, registering your object as a singleton with that context's BeanFactory using the fact that getBeanFactory returns an implementation of SingletonBeanRegistry.

    parentContext = new ClassPathXmlApplicationContext();
    parentContext.refresh(); //THIS IS REQUIRED
    parentContext.getBeanFactory().registerSingleton("myBean", myBean)
    

    Then specify this context as a parent to your "real" context The beans in the child context will then be able to refer to the bean in the parent.

    String[] fs = new String[] { "/path/to/myfile.xml" } 
    appContext = new FileSystemXmlApplicationContext(fs, parentContext);
    
    0 讨论(0)
提交回复
热议问题