在没有循环引用的情况下将MVP与Spring IOC相连? [英] Wiring MVP with Spring IOC without circular reference?

查看:96
本文介绍了在没有循环引用的情况下将MVP与Spring IOC相连?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的MVP应用程序中,我使用如下代码连接Presenter和View:

In my MVP applications I use code such as the following to wire my Presenter and View:

View view = new View();
Presenter presenter = new Presenter(view);
view.setPresenter(presenter);

View类是在暂时无效的状态下构造的,对setPresenter的调用可以纠正该状态.我在View类中有一些代码,如果在未配置Presenter的情况下使用View,则会抛出IllegalStateException.

The View class is constructed in a temporarily invalid state, which the call to setPresenter rectifies. I have some code in the View class that throws an IllegalStateException if the View is used without the Presenter being configured.

我希望Spring可以通过以下配置将这种关系连接起来:

I was hoping Spring could wire this relationship together with a configuration such as:

<bean id="presenter" class="com.foo.Presenter">
  <constructor-arg ref="view" />
</bean>

<bean id="view" class="com.foo.View">
  <property name="presenter" ref="presenter" />
</bean>

此操作失败,出现了长时间的循环依赖异常.

This failed with a lengthy circular-dependency exception.

有没有办法让Spring构造view bean,然后构造presenter bean,然后最终在view上调用setter?

Is there a way I can tell Spring to construct the view bean, then construct the presenter bean before finally calling the setter on view?

一个相关的问题是 Spring setter依赖注入后所有bean都已创建.但是,建议的解决方案之一是通过使用基于setter的接线来解决循环依赖性的,这正是我所要做的.我没有在这里做. 最新手册似乎也同意-参见标题为循环依赖项"的框:

A related question is Spring setter dependency injection after all beans have been created. However, one of the suggested solutions is to resolve the circular dependencies by using setter-based wiring, which is exactly what I'm failing to do here. The latest manual also seems to agree - see the box entitled "Circular dependencies":

一个可能的解决方案是编辑某些类的源代码,这些类的源代码由设置者而不是构造函数来配置.或者,避免构造函数注入,而仅使用setter注入.换句话说,尽管不建议这样做,但您可以使用setter注入配置循环依赖项.

推荐答案

一些进一步的研究已经发现了解决方案.最初,我尝试反转XML配置文件中Bean定义的顺序,并且可以正常工作:

Some further research has unearthed a solution. Initially, I tried reversing the order of the bean definitions in the XML config file and it worked:

<bean id="view" class="com.foo.View">
  <property name="presenter" ref="presenter" />
</bean>

<bean id="presenter" class="com.foo.Presenter">
  <constructor-arg ref="view" />
</bean>

但是,这感觉不对,因为我确信我不应该依赖文件排序来确保一切都不会中断.然后,这使人们意识到depends-on可以解决问题:

However, this felt wrong as I'm confident I shouldn't be relying on file ordering to ensure things aren't breaking. This then led to the realisation that the depends-on can solve the problem:

<bean id="presenter" class="com.foo.Presenter" depends-on="view">
  <constructor-arg ref="view" />
</bean>

<bean id="view" class="com.foo.View">
  <property name="presenter" ref="presenter" />
</bean>

我欢迎评论这是否是一种好方法.我以一种意想不到的方式将Spring屈服于我的意愿,这很合理.

I welcome comments on whether this is a good approach. It's quite plausible I'm bending Spring to my will in a way that's not intended.

这篇关于在没有循环引用的情况下将MVP与Spring IOC相连?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆