如何在从文件加载之前将bean注入ApplicationContext?

时间:2022-04-20 19:43:38

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

我有一个FileSystemXmlApplicationContext,我希望XML中定义的bean将构造函数参数作为未在Spring中声明的bean

For example, I would like to do:

例如,我想这样做:

<bean class="some.MyClass">
    <constructor-arg ref="myBean" />
</bean>

So I could imagine doing this via something like:

所以我可以想象这样做:

Object myBean = ...
context = new FileSystemXmlApplicationContext(xmlFile);
context.addBean("myBean", myBean); //add myBean before processing
context.refresh();

Except that there is no such method :-( does anyone know how I can achieve this?

除了没有这样的方法:-(有谁知道我怎么能做到这一点?

2 个解决方案

#1


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.

如何首先以编程方式创建一个空的父上下文,使用getBeanFactory返回SingletonBeanRegistry实现的事实将对象注册为具有该上下文的BeanFactory的单例。

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.

然后将此上下文指定为“真实”上下文的父级。子上下文中的bean将能够引用父级中的bean。

String[] fs = new String[] { "/path/to/myfile.xml" } 
appContext = new FileSystemXmlApplicationContext(fs, parentContext);

#2


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

由于我使用AnnotationConfigApplicationContext解决了这个问题,我发现了以下替代方案:

DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
beanFactory.registerSingleton("customBean", new CustomBean());
context = new AnnotationConfigApplicationContext(beanFactory);
context.register(ContextConfiguration.class);
context.refresh();

#1


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.

如何首先以编程方式创建一个空的父上下文,使用getBeanFactory返回SingletonBeanRegistry实现的事实将对象注册为具有该上下文的BeanFactory的单例。

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.

然后将此上下文指定为“真实”上下文的父级。子上下文中的bean将能够引用父级中的bean。

String[] fs = new String[] { "/path/to/myfile.xml" } 
appContext = new FileSystemXmlApplicationContext(fs, parentContext);

#2


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

由于我使用AnnotationConfigApplicationContext解决了这个问题,我发现了以下替代方案:

DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
beanFactory.registerSingleton("customBean", new CustomBean());
context = new AnnotationConfigApplicationContext(beanFactory);
context.register(ContextConfiguration.class);
context.refresh();