JSF and Dependency Injection

Last week i noticed that you can easily inject one managed bean into another in your JSF configuration file faces-config.xml. Even if this not new to all of you i will write some configuration details.

The following code injects an instance of managed bean edit into an instance of bean task. Value #{edit} is referencing the managed-bean-name edit.

<faces-config>
   ..
    <managed-bean>
        <managed-bean-name>task</managed-bean-name>
        <managed-bean-class>sernet.verinice.web.TaskBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
        <managed-property>
            <property-name>editBean</property-name>
            <property-class>sernet.verinice.web.EditBean</property-class>
            <value>#{edit}</value>
        </managed-property>
    </managed-bean>
    <managed-bean>
        <managed-bean-name>edit</managed-bean-name>
        <managed-bean-class>sernet.verinice.web.EditBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
   ..
</faces-config>

Class TaskBean needs a public getter and setter for EditBean:

public class TaskBean {

    private EditBean editBean;
    
    public EditBean getEditBean() {
        return editBean;
    }

    public void setEditBean(EditBean editBean) {
        this.editBean = editBean;
    }
}

The web container creates instances of TaskBean and EditBean for every user session. Without any „clue code“ the EditBean is set to the TaskBean instance by the container .

  1. No trackbacks yet.

Schreibe einen Kommentar

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s

%d Bloggern gefällt das: