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

后端 未结 3 1725
星月不相逢
星月不相逢 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: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();
    

提交回复
热议问题