IoC no Mentawai

0 respostas
saoj

O que vcs acharam disso:

Pontos positivos:

:arrow: Pode-se configurar as propriedades de cada componente. (WW não permite isso…)

:arrow: Pode-se usar componentes com construtores. (WW não permite isso…)

:arrow: Não precisa de interface enablers. (WW precisa de enablers…)

:arrow: Suporta injection por setter e por field privado ou publico.

:arrow: Suporta scopes ACTION, REQUEST, SESSION e APPLICATION. (Diferença do ACTION para o REQUEST é que o request atinge todas as actions encadeadas e o ACTION apenas uma action)

Me basiei na comparação feita entre o WW e o Spring aqui: http://www.theserverside.com/news/thread.tss?thread_id=21962

Pretendemos lançar isso na versão 1.1.

public interface Talker {
    
    public String saySomething();
    
}

public class UnpoliteTalker implements Talker {
    
    private String name;
    
    public UnpoliteTalker(String name) {
        this.name = name;
    }
    
    public String saySomething() {
        return "from " + name + " (" + toString() + "): Hello you #$#((%$#!";
    }
    
}

public class PoliteTalker implements Talker {
    
    private String name;
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String saySomething() {
        return "from " + name + " (" + toString() + "): Hello Sir!";
    }
    
}

Para usar esses carinhas com o IoC do Mentawai basta usar o IoCFilter junto com o InjectionFilter.

import org.mentawai.core.*;
import org.mentawai.filter.*;
import org.mentawai.ioc.*;
import examples.ioc.*;

public class ApplicationManager extends org.mentawai.core.ApplicationManager {
    
    private Component c1;
    private Component c2;
    
    public void init() {
        // IoC Components...
        DefaultComponent c1 = new DefaultComponent(PoliteTalker.class);
        c1.addProperty("name", "C1");
        
        DefaultComponent c2 = new DefaultComponent(UnpoliteTalker.class);
        c2.addInitValue("C2"); // for constructor...
        
        this.c1 = c1;
        this.c2 = c2;
    }
    
    public void loadActions() {
    
        ActionConfig ac = new ActionConfig("/HelloIoC", HelloIoC.class);
        ac.addConsequence(HelloIoC.SUCCESS, new Forward("/show.jsp"));
        addActionConfig(ac);
        
        // IoC	filters...	
        ac.addFilter(new IoCFilter(c1, "talker1", IoCFilter.ACTION));
        ac.addFilter(new IoCFilter(c2, "talker2", IoCFilter.SESSION));
        ac.addFilter(new IoCFilter(c2, "talker3", IoCFilter.APPLICATION));
        
        // Injection Filter needs to be used together with the IoCFilter...
        ac.addFilter(new InjectionFilter(true));
    }
}

Os componentes são injetados na action pelo InjectionFilter:

import java.util.*;

import org.mentawai.core.*;

public class HelloIoC extends BaseAction {
    
    private Talker talker1 = null;
    private Talker talker2 = null;
    private Talker talker3 = null;
    
    public void setTalker1(Talker t) {
        this.talker1 = t;
    }
    
    public void setTalker2(Talker t) {
        this.talker2 = t;
    }
    
    // t3 will be injected straight into the private field... (no need for setter)
    
    public String execute() throws ActionException {
        String s1 = null;
        String s2 = null;
        String s3 = null;
        
        if (talker1 != null) {
            s1 = talker1.saySomething();
            output.setValue("from_t1", s1);
        }
        if (talker2 != null) {
            s2 = talker2.saySomething();
            output.setValue("from_t2", s2);
        }
        if (talker3 != null) {
            s3 = talker3.saySomething();
            output.setValue("from_t3", s3);
        }
        return SUCCESS;
    }
}
Criado 27 de julho de 2005
Respostas 0
Participantes 1