Changes between Version 1 and Version 2 of HowTos/HowToUseBeanInjection

Show
Ignore:
Timestamp:
05/05/09 01:09:18 (17 years ago)
Author:
cfgrok (IP: 64.30.223.5)
Comment:

Escaped wiki page name links -- added missing link markup -- corrected typos

Legend:

Unmodified
Added
Removed
Modified
  • HowTos/HowToUseBeanInjection

    v1 v2  
    11= How To Use Bean Injection = 
    22 
    3 One of the time-saving new features of Model-Glue:Gesture is the capability to automatically inject model components that are managed by ColdSpring into the application's controllers. The term "bean injection" is used to describe this feature, as this is the ColdSpring term for a managed object (meaning that the object is created and cached by ColdSpring, and one "asks" ColdSpring for the object instead of instantiating it directly). A full explanation of ColdSpring is beyond the scope of this document, so examples of ColdSpring usage are intentionally kept extremely simple -- please refer to www.coldspringframework.org for more information. 
     3One of the time-saving new features of Model-Glue:Gesture is the capability to automatically inject model components that are managed by !ColdSpring into the application's controllers. The term "bean injection" is used to describe this feature, as this is the !ColdSpring term for a managed object (meaning that the object is created and cached by !ColdSpring, and one "asks" !ColdSpring for the object instead of instantiating it directly). A full explanation of !ColdSpring is beyond the scope of this document, so examples of !ColdSpring usage are intentionally kept extremely simple -- please refer to [http://www.coldspringframework.org/ www.coldspringframework.org] for more information. 
    44 
    5 In order to see how bean injection works, we will use the example model component from the Quickstart guide, PigLatinTranslator.cfc. In the Quickstart, we referenced this object in the TranslatePhrase function in our controller: 
     5In order to see how bean injection works, we will use the example model component from the Quickstart guide, !PigLatinTranslator.cfc. In the Quickstart, we referenced this object in the !TranslatePhrase function in our controller: 
    66 
    77{{{ 
     
    1818}}} 
    1919 
    20 In this case, we were creating the object directly using ColdFusion's createObject() function: 
     20In this case, we were creating the object directly using !ColdFusion's createObject() function: 
     21 
    2122{{{ 
    2223<cfset var translator = createObject("component", "translator.model.PigLatinTranslator").init("aeiou") /> 
    2324}}} 
    2425 
    25 In order to use bean injection instead, we would first need to add this object to the ColdSpring.xml configuration file. There is a comment in this file that indicates where to do so (although this is just a suggestion, not a requirement): 
     26In order to use bean injection instead, we would first need to add this object to the !ColdSpring.xml configuration file. There is a comment in this file that indicates where to do so (although this is just a suggestion, not a requirement): 
    2627 
    2728{{{ 
     
    3738</bean> 
    3839}}} 
     40 
    3941Without getting into too much detail, there are a couple of things to note here: 
    4042 
    41 First, we have used a bean id of "translator", which we will use to refer to the object within the beans scope. Next, the bean class is identical to the full CFC path used in the previous createObject() method. And finally, the argument passed into the init() method when creating the object is instead specified in the ColdSpring.xml file via the constructor-arg tag. For a more detailed explanation of ColdSpring's configuration syntax, please refer the the ColdSpring project site , and particularly the ColdSpring Quickstart guide. 
     43First, we have used a bean id of "translator", which we will use to refer to the object within the beans scope. Next, the bean class is identical to the full CFC path used in the previous createObject() method. And finally, the argument passed into the init() method when creating the object is instead specified in the !ColdSpring.xml file via the constructor-arg tag. For a more detailed explanation of !ColdSpring's configuration syntax, please refer the the [http://www.coldspringframework.org/ ColdSpring project site], and particularly the [http://www.coldspringframework.org/coldspring/examples/quickstart/ ColdSpring Quickstart guide]. 
    4244 
    43 Once the bean (object) has been defined in ColdSpring.xml, then it will be available for injection after a reload of the Model-Glue framework. There are actually two different ways that we can do this. 
     45Once the bean (object) has been defined in !ColdSpring.xml, then it will be available for injection after a reload of the Model-Glue framework. There are actually two different ways that we can do this. 
    4446 
    45 The first method is to add the bean id to the new, optional "beans" attribute of the controller tag in the ModelGlue.xml file. To see this in action, let's use the controller XML from the Quickstart guide: 
     47The first method is to add the bean id to the new, optional "beans" attribute of the controller tag in the !ModelGlue.xml file. To see this in action, let's use the controller XML from the Quickstart guide: 
     48 
    4649{{{ 
    4750<controller name="MyController" type="translator.controller.Controller" beans="translator"> 
     
    5154</controller> 
    5255}}} 
    53 Note the addition of the "beans" attribute, with value of "translator", which is the id of the bean in ColdSpring.xml. In this case we are only injecting a single object, but multiple objects can be injected by supplying a comma-delimited list of bean ids. 
    5456 
    55 The second method would be to instead add the "beans" attribute directly to the controller's cfccomponent tag: 
     57Note the addition of the "beans" attribute, with value of "translator", which is the id of the bean in !ColdSpring.xml. In this case we are only injecting a single object, but multiple objects can be injected by supplying a comma-delimited list of bean ids. 
     58 
     59The second method would be to instead add the "beans" attribute directly to the controller's cfcomponent tag: 
     60 
    5661{{{ 
    5762<cfcomponent output="false" hint="I am a Model-Glue controller." extends="ModelGlue.gesture.controller.Controller" beans="translator"> 
    5863}}} 
     64 
    5965This has the exact same result, so we can choose whichever option appeals to us. In either case, objects that are injected are subsequently available to Model-Glue controllers via the new "beans" scope, so we can refer to our model object with the reference "beans.translator". Here is the controller function from the Quickstart with the necessary change: 
     66 
    6067{{{ 
    6168<cffunction name="TranslatePhrase" access="public" returntype="void" output="false"> 
     
    6976</cffunction> 
    7077}}} 
    71 So in this case, the object creation is handled by ColdSpring, and we are given a reference to the object to work with in our controller. Note that it is not necessary to set the object into a function-local (var-scoped) variable, as we could also refer to the object in the beans scope directly, if desired: 
    7278 
    73 And that's it! Once your model objects are defined in ColdSpring, you can access them in any Model-Glue controller simply by adding the beans attribute and calling them via the beans scope. 
    74 Switching to Bean Injection in an Upgraded Model-Glue 2 Application 
    75 If you are already using ColdSpring-managed beans in your Model-Glue:Unity application, then you are most likely using one of two existing methodologies to access them in your controllers: the getBean() method or autowiring. Note that both of these approaches are still supported in Model-Glue:Gesture, so you do not have to change anything in order to upgrade -- it will continue to work without needing any alterations. 
     79So in this case, the object creation is handled by !ColdSpring, and we are given a reference to the object to work with in our controller. And that's it! Once your model objects are defined in !ColdSpring, you can access them in any Model-Glue controller simply by adding the beans attribute and calling them via the beans scope. 
     80 
     81== Switching to Bean Injection in an Upgraded Model-Glue 2 Application == 
     82 
     83If you are already using !ColdSpring-managed beans in your Model-Glue:Unity application, then you are most likely using one of two existing methodologies to access them in your controllers: the getBean() method or autowiring. Note that both of these approaches are still supported in Model-Glue:Gesture, so you do not have to change anything in order to upgrade -- it will continue to work without needing any alterations. 
    7684 
    7785For anyone who is unfamiliar with either of these concepts, here is a quick summary: 
     86 
    7887=== getModelGlue().getBean() === 
    7988 
    8089The older of the two previous options is the getModelGlue().getBean() method. This function is available in the Model-Glue framework API, and can be called anywhere in a Model-Glue controller, passing in the bean id as the single argument: 
     90 
    8191{{{ 
    8292<cfset var translator = getModelGlue().getBean("translator") /> 
     
    8595Note that this would either require that the getBean() call be used in every place that a model object is needed, or that an init() method be defined for the controller component in order to set the model objects into the controller's private variables scope for use in other methods. 
    8696 
     97If an init() method is defined in a controller, it is also necessary to call super.init(), passing in the argument that was passed into the method by the framework. As an example, here is the init() method demonstrated in the application template: 
    8798 
    88 If an init() method is defined in a controller, it is also necessary to call super.init(), passing in the argument that was passed into the method by the framework. As an example, here is the init() method demonstrated in the application template: 
    8999{{{ 
    90100<cffunction name="init" access="public" output="false" hint="Constructor"> 
     
    99109=== Autowiring === 
    100110 
    101 Autowiring added a layer of abstraction, whereby instead of calling a model object directly, one would instead define a setter method in the controller that follows a naming convention of "set[bean id]", and Model-Glue will "autowire" the controller with any matching beans in the ColdSpring definition that have a matching id: 
     111Autowiring added a layer of abstraction, whereby instead of calling a model object directly, one would instead define a setter method in the controller that follows a naming convention of "set[bean id]", and Model-Glue will "autowire" the controller with any matching beans in the !ColdSpring definition that have a matching id: 
    102112 
    103113{{{ 
     
    169179 
    170180And the individual references to the object via the getTranslator() method would remain unchanged. 
    171  
    172