Spring Book – Chapter 11 – Spring Web

 

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:

  1. Configure Spring DispatcherServlet in web.xml.
  2. Configure the BlazeDS message broker as a Spring-managed bean in the application context configuration file.
  3. Map all the message broker requests to the DispatcherServlet.
  4. 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

[xml]……

<servlet>

<servlet-name>spring-flex</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/applicationContext.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

…..[/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"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:flex="http://www.springframework.org/schema/flex"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/flex

http://www.springframework.org/schema/flex/spring-flex-1.0.xsd">

….

<flex:message-broker/>

….

</beans>[/xml]

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

[xml]….

<servlet-mapping>

<servlet-name>spring-flex</servlet-name>

<url-pattern>/messagebroker/*</url-pattern>

</servlet-mapping>

…..[/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

[xml]….

<bean id="loyaltyService" class="com.mybook.loyalty.services.LoyaltyService" >

<flex:remoting-destination />

<constructor-arg ref="dataSource" />

</bean>

<bean id="dataSource"

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

…..

</bean>

….[/xml]

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

[xml]<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/spring/applicationContext.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<servlet>

<servlet-name>spring-flex</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>spring-flex</servlet-name>

<url-pattern>/messagebroker/*</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>spring-mvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>spring-mvc</servlet-name>

<url-pattern>/spring/*</url-pattern>

</servlet-mapping>[/xml]

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: 2287

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

1 thought on “Spring Book – Chapter 11 – Spring Web

Leave a Reply

Your email address will not be published. Required fields are marked *