Spring:将启动ApplicationContext的对象注入到ApplicationContext中
我想在遗留应用程序中使用 Spring。
核心部分是一个类,我们称之为LegacyPlugin,它代表应用程序中的一种可插入部分。问题是这个类也是数据库连接器,并且用于创建许多其他对象,通常通过构造函数注入...
我想从 LegacyPlugin 启动一个 ApplicationContext,并将其注入到ApplicationContext(例如通过 BeanFactory)来创建其他对象。然后代码将被重写,以使用 setter 注入和很快。
我想知道实现这一目标的最佳方法是什么。到目前为止,我有一个使用 BeanFactory 的工作版本,它使用 ThreadLocal 来保存对当前执行的插件的静态引用,但对我来说似乎很难看......
下面是我想要最终得到的代码:
public class MyPlugin extends LegacyPlugin {
public void execute() {
ApplicationContext ctx = new ClassPathXmlApplicationContext();
// Do something here with this, but what ?
ctx.setConfigLocation("context.xml");
ctx.refresh();
}
}
<!-- This should return the object that launched the context -->
<bean id="plugin" class="my.package.LegacyPluginFactoryBean" />
<bean id="someBean" class="my.package.SomeClass">
<constructor-arg><ref bean="plugin"/></constructor-arg>
</bean>
<bean id="someOtherBean" class="my.package.SomeOtherClass">
<constructor-arg><ref bean="plugin"/></constructor-arg>
</bean>
I want to use Spring inside a legacy application.
The core piece is a class, let's call it LegacyPlugin, that represents a sort of pluggable piece in the application. The problem is that this class is also the database connector, and is used to create lots of other objects, often via constructor injection...
I want to launch an ApplicationContext from the LegacyPlugin, and inject it into the ApplicationContext, via a BeanFactory for example, to create the other objects. The code will then be rewritten, to use setter injection & so on.
I would like to know what is the best way to achieve this. So far, I have a working version using a BeanFactory that uses a ThreadLocal to hold a static reference to the plugin currently executed, but it seems ugly to me...
Below is the code I would like to end up with :
public class MyPlugin extends LegacyPlugin {
public void execute() {
ApplicationContext ctx = new ClassPathXmlApplicationContext();
// Do something here with this, but what ?
ctx.setConfigLocation("context.xml");
ctx.refresh();
}
}
<!-- This should return the object that launched the context -->
<bean id="plugin" class="my.package.LegacyPluginFactoryBean" />
<bean id="someBean" class="my.package.SomeClass">
<constructor-arg><ref bean="plugin"/></constructor-arg>
</bean>
<bean id="someOtherBean" class="my.package.SomeOtherClass">
<constructor-arg><ref bean="plugin"/></constructor-arg>
</bean>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SingletonBeanRegistry
接口允许您通过其registerSingleton
方法手动将预配置的单例注入到上下文中,如下所示:这会添加
plugin
bean结合上下文。您不需要在context.xml
文件中声明它。The
SingletonBeanRegistry
interface allows you to manually inject a pre-configured singleton into the context via itsregisterSingleton
method, like this:This adds the
plugin
bean to the context. You don't need to declare it in thecontext.xml
file.实际上,这不起作用......它会导致以下错误:
最终的解决方案是使用 GenericApplicationContext :
Actually, this doesn't work... It causes the following error :
The final solution is to use GenericApplicationContext :