3.4. How Do You Use It?

Now that you have the component created, you will add services to it as you would to any MXML file. The following code contains examples of adding each type of service that the ServiceLocator can use:

<?xml version="1.0" encoding="utf-8"?>
<services:ServiceLocator xmlns:services="com.adobe.cairngorm.business.*"
xmlsn:mx="http://www.adobe.com/2006/mxml">
  <mx:HTTPService id="someHTTPService" url="http://somesite.com/someService"
method="POST" resultFormat="xml" contentType="application/xml" />
  <mx:RemoteObject id="someRemoteObject" destination="someDestination"
source="com.somepackage.someservice" />
<mx:WebService id="someWebService" wsdl=" http://somesite.com/some.wsdl" />
</services:ServiceLocator>

In order to use the ServiceLocator in your components, there must be an instance of it in your application, normally in the main application file.

To include the ServiceLocator component you created, create a namespace in the main application tag pointing to the location where you created the component. Then simply add an instance of the ServiceLocator you created to the main application, as in the following:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
xmlsn:business="com.cairngormexample.business.*">
  <business:CairngormExampleServices id="services"/>
</mx:Application>

To access a service contained in the ServiceLocator, use the following code:

//HTTPService var service:HTTPService = ServiceLocator.getInstance().getHTTPService('someHTTPService); ...

Get Professional Cairngorm™ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.