Spring:将启动ApplicationContext的对象注入到ApplicationContext中

发布于 2024-10-28 06:12:30 字数 1169 浏览 1 评论 0原文

我想在遗留应用程序中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲爱恨情仇 2024-11-04 06:12:30

SingletonBeanRegistry 接口允许您通过其 registerSingleton 方法手动将预配置的单例注入到上下文中,如下所示:

ApplicationContext ctx = new ClassPathXmlApplicationContext();
ctx.setConfigLocation("context.xml");

SingletonBeanRegistry beanRegistry = ctx.getBeanFactory();
beanRegistry.registerSingleton("plugin", this);

ctx.refresh();

这会添加 plugin bean结合上下文。您不需要在 context.xml 文件中声明它。

The SingletonBeanRegistry interface allows you to manually inject a pre-configured singleton into the context via its registerSingleton method, like this:

ApplicationContext ctx = new ClassPathXmlApplicationContext();
ctx.setConfigLocation("context.xml");

SingletonBeanRegistry beanRegistry = ctx.getBeanFactory();
beanRegistry.registerSingleton("plugin", this);

ctx.refresh();

This adds the plugin bean to the context. You don't need to declare it in the context.xml file.

孤独患者 2024-11-04 06:12:30

实际上,这不起作用......它会导致以下错误:

BeanFactory not initialized or already closed
call 'refresh' before accessing beans via the ApplicationContext

最终的解决方案是使用 GenericApplicationContext

GenericApplicationContext ctx = new GenericApplicationContext();
ctx.getBeanFactory().registerSingleton("plugin", this);
new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(
    new ClassPathResource("context.xml"));
ctx.refresh();

Actually, this doesn't work... It causes the following error :

BeanFactory not initialized or already closed
call 'refresh' before accessing beans via the ApplicationContext

The final solution is to use GenericApplicationContext :

GenericApplicationContext ctx = new GenericApplicationContext();
ctx.getBeanFactory().registerSingleton("plugin", this);
new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(
    new ClassPathResource("context.xml"));
ctx.refresh();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文