5.2. Remoting to Spring Beans

Existing and new Spring beans can be exposed as Flex remoting service destinations. Prior to the availability of the Spring BlazeDS project, it was common to write a custom factory to get access to the Spring beans. Spring beans are managed objects and in standard BlazeDS instantiating the beans and maintaining their lifecycle had to be delegated to an external Spring framework container. With Spring BlazeDS, using Spring beans as remoting destinations is considerably simplified.

Let's take a simple example to explain things a bit further. I create a simple Spring bean, which I call BookService. The BookService allows me to perform the create, read, update, and delete (CRUD) operations on a catalog of books. Each book is identified by four simple attributes:

  • Id (a catalog specific identifier)

  • Title

  • Author (I make an assumption that each book in the simple example has only one author)

  • Price

In a real-life book catalog, you are likely to find other attributes like the name of the publisher, the year of publication, and the two types of ISBN numbers for a book. To keeps things simple and to keep the focus on the core aspects of remoting Spring beans to Flex, all these additional attributes are not included in the simple example.

In the simple example, a Book could be modeled as a POJO, which could be as follows:

package problazeds.ch05;

import java.io.Serializable;

public class Book implements Serializable {

    static final long serialVersionUID = 103844514947365244L;

    private int bookId;
    private String title;
    private String author;
    private double price;


    public Book()
    {
    }

    public int getBookId()
    {
        return bookId;
    }

    public void setBookId(int bookId)

{
        this.bookId = bookId;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

    public String getAuthor()
    {
        return author;
    }

    public void setAuthor(String author)
    {
        this.author = author;
    }

    public double getPrice()
    {
        return price;
    }

    public void setPrice(double price)
    {
        this.price = price;
    }

}

Next, a BookService could be created to perform CRUD operations on the collection of Book objects in the books catalog. Using a clean design, it may be prudent to create an IBookService interface to define the behavior of the BookService. That way, there could be alternative implementations to support the same contract. The IBookService interface is shown in Listing 5-1.

Example 5.1. IBookService Interface
package problazeds.ch05;

import java.util.List;

public interface IBookService
{
    public List<Book> findAll();

    public Book findById(int id);

public Book create(Book item);

    public boolean update(Book item);

    public boolean remove(Book item);

}

In traditional BlazeDS implementations that do not involve Spring BlazeDS, you would configure the BookService as a remoting service destination in the remoting-config.xml file. The remoting-config.xml file contains all definitions related to the remoting service in BlazeDS. This XML file is included in services-config.xml by reference. The configuration for the BookService remoting destination in remoting-config.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service"
class="flex.messaging.services.RemotingService">
    <adapters>
        <adapter-definition id="java-object"
            class="flex.messaging.services.remoting.adapters.JavaAdapter"
            default="true"/>
    </adapters>

    <default-channels>
        <channel ref="my-amf"/>
    </default-channels>

    <destination id="bookService">
        <properties>
            <source>problazeds.ch05.BookService</source>
            <scope>session</scope>
        </properties>
        <adapter ref="java-object" />
    </destination>

</service>

Using Spring BlazeDS, the BookService Spring bean is first configured either in the DispatcherServlet configuration or the services context configuration as follows:

<bean id="bookService" class="problazeds.ch05.BookService" />

Following this, the BookService can be exposed as a remoting service destination by configuring it as follows:

<flex:remoting-destination ref="bookService" />

Once again convention is preferred over configuration. Following a standard convention and going with defaults is more effective and efficient than configuring every single configurable element. A lot of configuration is usually standard boilerplate and one easily avoids extra work if convention is followed. The Java adapter class and the AMF channel are configured by default.

You can also specify the adapter and channel defaults, like so:

<flex:message-broker>
     <flex:remoting-service
        default-adapter-id="my-alternative-default-remoting-adapter"
        default-channels="my-amf, my-secure-amf" />
</flex:message-broker>

In the remoting-destination configuration, you can also specify the list of methods to include and exclude. If you desire to allow read-only access to the books catalog, you can configure the remoting destination as follows:

<flex:remoting-destination
    ref="bookService"
    include-methods="findAll, findById"
    exclude-methods="create, update, remove" />

In addition to the advanced configuration, you also have the choice of specifying the remoting destination characteristics as an inline property. Therefore, the last remoting-destination configuration can be combined with the bean configuration, like so:

<bean id="bookService" class="problazeds.ch05.BookService">
    <flex:remoting-destination
        ref="bookService"
        include-methods="findAll, findById"
        exclude-methods="create, update, remove" />
</bean>

At this stage, you may have realized that with Spring BlazeDS you do not need to configure remoting destinations in remoting-config.xml.

The Spring style remoting-destination configuration is easier, intuitive, and terse. The remoting-destination configuration is supported under the hood by the org.springframework.flex.remoting.RemotingDestinationExporter factory class that creates the Spring managed remoting destinations for a Flex client.

Your last configuration using RemotingDestinationExporter explicitly is:

<bean id="book"
    class="org.springframework.flex.remoting.RemotingDestinationExporter">
    <property name="messageBroker" ref="_messageBroker"/>
    <property name="service" ref="bookService"/>
    <property name="destinationId" value="bookService"/>
    <property name="includeMethods" value="findAll, findById"/>
    <property name="excludeMethods" value="create, update, remove"/>
</bean>

So far, all configurations have been done using XML. You could also use Java annotations to achieve the same result. The BookService class with annotations for exposing it as a BlazeDS remoting destination is shown in Listing 5-2.

Example 5.2. BookService Remoting Destination Class
package problazeds.ch05;
//import statements

@Service("productService")
@RemotingDestination(channels={"my-amf","my-secure-amf"})
public class BookService implements IBookService
{
    private final DataSource dataSource;

    public BookService(DataSource dataSource)
    {
        this.dataSource = dataSource;
    }

    @RemotingExclude
    public Book create(Book item)
    {
        //Implementation not shown in the listing
    }

    @RemotingInclude
    public List<Book> findAll()
    {
        //Implementation not shown in the listing
    }

    @RemotingInclude
    public Book findById(int id)
    {
        //Implementation not shown in the listing
    }

    @RemotingExclude
    public boolean remove(Book item)
    {
        //Implementation not shown in the listing
    }

    @RemotingExclude
    public boolean update(Book item)
    {
        //Implementation not shown in the listing
    }

}

BlazeDS provides two important services, namely remoting and messaging. This section explained how you could use a Spring bean as a remoting destination. The next section will explain how BlazeDS messaging could work with the Spring messaging services.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.128.105