Spring BlazeDS Integration
Spring BlazeDS Integration is an answer to the communities demand for a top-level solution for building Spring-powered RIA (Rich Internet Applications) using Adobe Flex for the client-side technology. Though the integration was previously possible, it has not been a natural one all this while. Spring BlazeDS integration makes this bonding natural and it does this by allowing it to connect Flex clients with Spring managed services using BlazeDS MessageBroker boot-strapped by Spring. The project nowadays is also called as Spring Flex.
HTTP messages from the Flex client will be routed through the Spring DispatcherServlet to the Spring-managed MessageBroker.
BlazeDS is an open source project from Adobe which provides the remoting and messaging foundation for connecting a Flex-based front-end to Java back-end services. BlaseDS gives your Flex applications additional options for data connectivity. Without BlazeDS, Flex applications can access back-end data using either the HTTPService or WebService components.
Configuring and Using BlazeDS MessageBroker with Spring
The central component that must be configured to use Spring BlazeDS Integration is the MessageBroker. It is configured and bootstrapped as a Spring-managed bean and doesn’t need to be configured in web.xml. Remote objects are configured the “Spring way” in the application context configuration file. Similarly, Messaging destinations are configured the “Spring way”. Using Spring security integration ensures that you can secure Spring-managed beans exposed as remote objects just as you would secure any other Spring-managed endpoint, and that you can provide your security credentials from within a Flex application. We will be covering this in Chapter 15 in detail.
To set up a Spring BlazeDS Integration application, the steps to be performed are as follows:
- Configure Spring DispatcherServlet in web.xml.
- Configure the BlazeDS message broker as a Spring-managed bean in the application context configuration file.
- Map all the message broker requests to the DispatcherServlet.
- Configure your beans and expose them as remote objects in the application context configuration file.
The following sections expand these steps in detail with code snippets to make these concepts and configurations clear in all aspects.
Spring DispatcherServlet Configuration
In web.xml, you configure the DispatcherServlet to bootstrap the Spring WebApplicationContext as shown in Listing 11-11 below.
Listing 11-11. Spring DispatcherServlet configuration in web.xml
BlaseDS MessageBroker Configuration
You will first have to declare the various XSD declaration in your Spring configuration file, applicationContext.xml. A relevant part of this file is as shown in Listing 11-12 below.
Listing 11-12. XSD declaration in spring configuration file applicationContext.xml
[xml]<?xml version="1.0" encoding="UTF-8"?>
XSD (XML Schema Document) is used to express a set of rules to which an XML document must conform in order to be considered ‘valid’ according to that schema.
Mapping Requests to MessageBroker
You then map all the message broker requests to the DispatcherServlet as shown in Listing 11-13 in web.xml below.
Listing 11-13. Mapping all requests of message broker to DispatcherServlet in web.xml
Configure beans and its properties
Configure the beans which you want to your in your Flex application as shown in Listing 11-14 in your applicationContext.xml file.
Listing 11-14. Bean configuration in applicationContext.xml
<bean id="loyaltyService" class="com.mybook.loyalty.services.LoyaltyService" >
<constructor-arg ref="dataSource" />
Using Flex Clients alongside Spring MVC Controllers
As the usual case may be, if your application needs to serve more than just Flex-based clients, you can achieve it by having a different mapping strategy, which can be done in your web application deployment file, web.xml as shown in Listing 11-15 below.
Listing 11-15. Mapping strategy for having clients other than Flex in your web application
The approach is very simple to understand, you first configure your main application layer (services, security, supporting infrastructure, etc) in a parent context loaded via the ContextLoaderListener, and then configure all aspects of your Spring Flex controllers in one child DispatcherServlet(spring-flex) context, and all aspects specific to your spring MVC controllers in a separate child DispatcherServlet(spring-mvc) context.
Page Visitors: 2139