Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for 
change notification.

The following page has been changed by ShingHingMan:
http://wiki.apache.org/tapestry/InjectPropertyIntoASO

New page:
''' How to inject a property into an Application State Object (ASO) ? '''

In order to inject properties into an ASO, we implement a custom 
State``Object``Factory and define the method State``Object``Factory.create to 
create the ASO with the desired injected properties. We then use the above 
custom State``Object``Factory to create our ASO. The following example 
illustrates the above in detail. Suppose we have an ASO My``ASO with property 
messageHolder. We would like to create an My``ASO with the property 
messageHolder being injected by Hive``Mind.


{{{
package man.aso;

public class MyASO {

        private IMessageHolder messageHolder;

        public IMessageHolder getMessageHolder() {
                return messageHolder;
        }

        public void setMessageHolder(IMessageHolder message) {
                this.messageHolder = message;
        }
}
}}}

{{{
package man.aso;

public interface IMessageHolder {

        public String getMessage();

}
}}}

{{{
package man.aso;

public class HelloMessageHolder implements IMessageHolder{

        public String getMessage() {
                return "HelloMessage : Hello ";
        }

}

}}}


We shall inject Hell``oMessage``Holder into My``ASO. We now implement a custom 
State``Object``Factory called My``ASO``Factory.

{{{
package man.aso;

import org.apache.tapestry.engine.state.StateObjectFactory;

public class MyASOFactory implements StateObjectFactory{

        // To be injected by HiveMind
        private IMessageHolder messageHolder;
        
        // Contruct an MyASO with injected messageHolder
        public Object createStateObject() {     
                MyASO myASO = new MyASO();
                myASO.setMessageHolder(getMessageHolder());
                return myASO;
        }

        public IMessageHolder getMessageHolder() {              
                return messageHolder;
        }

        public void setMessageHolder(IMessageHolder message) {
                this.messageHolder = message;
        }

}

}}}


Note that the property My``ASO``Factory.messageHolder will be injected by 
Hive``Mind. The following configuration in Hive``Mind module descriptor 
hivemodule.xml does all the wiring.

{{{
hivemodule.xml

<service-point id="helloMessage" interface="man.aso.IMessageHolder">
        <invoke-factory>
        <construct class="man.aso.HelloMessageHolder"/>                 
       </invoke-factory>                                        
</service-point>
        
 <!-- This service creates an StateObjectFactory to create a MyASO.
         The property MyASO.messageHolder will be injected from service
         point helloMessage.    
   -->
<service-point id="myASOFactory" interface="man.aso.MyASOFactory">
    <invoke-factory>
         <construct class="man.aso.MyASOFactory">
                  <set-service property="messageHolder" 
service-id="helloMessage"/>
         </construct>
   </invoke-factory>  
</service-point>
        
<contribution configuration-id="tapestry.state.ApplicationObjects">
    <state-object name="myASO" scope="session">
        <invoke-factory object="service:myASOFactory" />
     </state-object>
</contribution>

}}}


The above will result in an ASO my``ASO being created with property 
messageHolder set to Hello``Message``Holder. 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to