Getting Spring IOC to work with MVP pattern

I am trying to use an MVP design pattern using a Swing application in conjunction with Spring IOC. In MVP View, I have to pass myself to Presenter, and I cannot figure out how to do this with Spring.

public class MainView  implements IMainView {

    private MainPresenter _presenter;

    public MainView() {

        _presenter = new MainPresenter(this,new MyService());

       //I want something more like this
       // _presenter = BeanFactory.GetBean(MainPresenter.class);

    }

}

This is my xml configuration (wrong)

<bean id="MainView" class="Foo.MainView"/>
<bean id="MyService" class="Foo.MyService"/>

<bean id="MainPresenter" class="Foo.MainPresenter">
    <!--I want something like this, but this is creating a new instance of View, which is no good-->
   <constructor-arg type="IMainView">
        <ref bean="MainView"/>
    </constructor-arg>
    <constructor-arg  type="Foo.IMyService">
        <ref bean="MyService"/>
     </constructor-arg>
</bean>

How to get a presentation in the presenter?

+1
source share
1 answer

, bean, BeanFactory.getBean(String name, Object... args). , bean, , , setter MyService:

 public class MainView  implements IMainView { 

    private MainPresenter _presenter; 

    public MainView() { 
        _presenter = beanFactory.getBean("MainPresenter", this); 
    }  
}

prototype, MainView MainPresenter

<bean id="MyService" class="Foo.MyService"/>   

<bean id="MainPresenter" class="Foo.MainPresenter" scope = "prototype">   
    <constructor-arg type="IMainView"><null /></constructor-arg>   
    <property name = "myService">   
        <ref bean="MyService"/>   
    </property>   
</bean>
+2

Source: https://habr.com/ru/post/1728791/


All Articles