4. Guice Services
Introduction
Granite Data Services brings support for Google Guice and Wideplay Warp services.
For a complete sample with Granite DS/Guice/Warp/EJB 3 entity beans working together, download graniteds-***.zip and import examples/graniteds_guice as a new Eclipse project.
Guice Services Configuration
In order to access your Guice services from your Flex application, you must configure your Flex services as follows:
<services-config> <services> <service id="granite-service" class="flex.messaging.services.RemotingService" messageTypes="flex.messaging.messages.RemotingMessage"> <destination id="cars"> <channels> <channel ref="my-graniteamf"/> </channels> <properties> <factory>guiceFactory</factory> <source>test.granite.guice.services.Cars</source> </properties> </destination> </service> </services> <factories> <factory id="guiceFactory" class="org.granite.guice.GuiceServiceFactory" /> </factories> <channels> <channel-definition id="my-graniteamf" class="mx.messaging.channels.AMFChannel"> <endpoint uri="http://{server.name}:{server.port}/{context.root}/graniteamf/amf" class="flex.messaging.endpoints.AMFEndpoint"/> </channel-definition> </channels> </services-config>
The org.granite.guice.GuiceServiceFactory is declared in the <fatory> node and used in the "cars" destination. See <service> node. The <source> node then declares the test.granite.guice.services.Cars class as a Guice service. See this class in examples/graniteds_guice for @Inject and @Transactional annotations usage.
Additional Web Configuration (web.xml)
In order to initialize Guice and Warp persistence, you must also customize your web.xml as follows:
<web-app version="2.4" ...> ... <!-- Guice specific context listener (initialize Guice injector and WARP persistence) --> <listener> <listener-class>test.granite.guice.listeners.InitNDestroy</listener-class> </listener> <!-- WARP persistence filter --> <filter> <filter-name>sessionPerRequestFilter</filter-name> <filter-class>com.wideplay.warp.jpa.SessionPerRequestFilter</filter-class> </filter> <filter-mapping> <filter-name>sessionPerRequestFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ... </web-app>
The InitNDestroy listener purpose is to initialize a Guice injector and Warp/Hibernate persistence. See details in examples/graniteds_guice.
