Changes between Version 1 and Version 2 of HowTos/HowToUseBeanInjection
- Timestamp:
- 05/05/09 01:09:18 (17 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
HowTos/HowToUseBeanInjection
v1 v2 1 1 = How To Use Bean Injection = 2 2 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.orgfor more information.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 [http://www.coldspringframework.org/ www.coldspringframework.org] for more information. 4 4 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 theTranslatePhrase function in our controller: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: 6 6 7 7 {{{ … … 18 18 }}} 19 19 20 In this case, we were creating the object directly using ColdFusion's createObject() function: 20 In this case, we were creating the object directly using !ColdFusion's createObject() function: 21 21 22 {{{ 22 23 <cfset var translator = createObject("component", "translator.model.PigLatinTranslator").init("aeiou") /> 23 24 }}} 24 25 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):26 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): 26 27 27 28 {{{ … … 37 38 </bean> 38 39 }}} 40 39 41 Without getting into too much detail, there are a couple of things to note here: 40 42 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.43 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 [http://www.coldspringframework.org/ ColdSpring project site], and particularly the [http://www.coldspringframework.org/coldspring/examples/quickstart/ ColdSpring Quickstart guide]. 42 44 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.45 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. 44 46 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: 47 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: 48 46 49 {{{ 47 50 <controller name="MyController" type="translator.controller.Controller" beans="translator"> … … 51 54 </controller> 52 55 }}} 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.54 56 55 The second method would be to instead add the "beans" attribute directly to the controller's cfccomponent tag: 57 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. 58 59 The second method would be to instead add the "beans" attribute directly to the controller's cfcomponent tag: 60 56 61 {{{ 57 62 <cfcomponent output="false" hint="I am a Model-Glue controller." extends="ModelGlue.gesture.controller.Controller" beans="translator"> 58 63 }}} 64 59 65 This 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 60 67 {{{ 61 68 <cffunction name="TranslatePhrase" access="public" returntype="void" output="false"> … … 69 76 </cffunction> 70 77 }}} 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:72 78 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. 79 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. 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 83 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. 76 84 77 85 For anyone who is unfamiliar with either of these concepts, here is a quick summary: 86 78 87 === getModelGlue().getBean() === 79 88 80 89 The 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 81 91 {{{ 82 92 <cfset var translator = getModelGlue().getBean("translator") /> … … 85 95 Note 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. 86 96 97 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: 87 98 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:89 99 {{{ 90 100 <cffunction name="init" access="public" output="false" hint="Constructor"> … … 99 109 === Autowiring === 100 110 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:111 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: 102 112 103 113 {{{ … … 169 179 170 180 And the individual references to the object via the getTranslator() method would remain unchanged. 171 172
![(please configure the [header_logo] section in trac.ini)](/ModelGlue.com/trac.cgi/chrome/site/your_project_logo.png)