Spring Configuration for Flex


Then follow these steps to configure a new Spring project:

1. Download the Spring Framework from http://www.springframework.org/download.


2. Copy the spring.jar file that you downloaded to the AppFoundation/WebRoot/WEB-INF/
lib directory in the af_Central project which contains the BlazeDS server, and add it to
the Java Build Path in the project properties. In the root/libs directory for the project,
you should keep only the files needed for compilation, not those for deployment. That
way, you are not maintaining multiple locations for JAR libraries.


3. Modify the web.xml file on the af_Central BlazeDS server and add the security listener,
filter, filter-mapping, and context-param definitions, as follows:


4. In the Spring project in Eclipse, register the SBI front controller in AppFoundation/
WebRoot/WEB-INF/flex/services-config.xml


The web.xml Configuration for Spring Security and SBI Server


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.// ➥
DTD Web Application 2.3//EN" ➥
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>AF Central WS</display-name>
<description>AF Central WS</description>
<!-- Http Flex Session attribute and binding listener support -->
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>
<!-- The front controller of this Spring Web application,
responsible for handling all application requests -->
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</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>

<!-- Map all *.spring requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/springSecurityContext.xml
</param-value>
</context-param>
</web-app>

We are going to use the security modules

The springSecurityContext.xml File


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-2.0.xsd">
<security:http auto-config="true">
<security:intercept-url pattern="index.html" filters="none"/>
<security:intercept-url pattern="*.swf" filters="none"/>
<security:intercept-url pattern="*.html" access="ROLE_USER"/>
</security:http>
<security:authentication-provider>
<security:user-service>
<security:user name="katiebug" password="test"
authorities="ROLE_USER, ROLE_ADMIN"/>
<security:user name="colie" password="test"
authorities="ROLE_USER"/>
</security:user-service>
</security:authentication-provider>
</beans>


Hibernate Configuration for Spring


AF – Client Manager application, we will use Hibernate 3 with annotations
to deliver the payload to and from our database. To make this happen, we need to configure applicationContext.xml to implement Hibernate, annotated classes, and transact ion management.

The hibApplication.properties File


# JDBC Connection information
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/apress
jdbc.username=af

jdbc.password=afpass
# Hibernate 3 configuration
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.transaction.factory_class=org.hibernate.transaction.JDBCTransactionFactory
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50



The applicationContext.xml File Using Hibernate 3 with Annotations and
Transaction Management


<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-lazy-init="true">
<!-- START Load application properties -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>WEB-INF/hibApplication.properties</value>
</property>
</bean>
<!-- END Load application properties -->
<!-- START HIBERNATE CONFIG -->
<!-- Configure SessionFactory -->
<bean id="sessionFactory" class= ➥
"org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="annotatedClasses">
<list>
<!-- Add annotated domain objects here-->
</list>
</property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.transaction.factory_class">
${hibernate.transaction.factory_class}
</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.c3p0.min_size">
${hibernate.c3p0.min_size}
</prop>
<prop key="hibernate.c3p0.max_size">
${hibernate.c3p0.max_size}
</prop>
<prop key="hibernate.c3p0.timeout">
${hibernate.c3p0.timeout}
</prop>
<prop key="hibernate.c3p0.max_statements">
${hibernate.c3p0.max_statements}
</prop>
<prop key="hibernate.connection.driver_class">
${jdbc.driverClassName}
</prop>
<prop key="hibernate.connection.url">${jdbc.url}</prop>
<prop key="hibernate.connection.username">
${jdbc.username}
</prop>
<prop key="hibernate.connection.password">
${jdbc.password}
</prop>
</props>
</property>
</bean>
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean class="org.springframework.beans.factory.annotation. ➥
RequiredAnnotationBeanPostProcessor"/>
<!-- END HIBERNATE CONFIG -->

<!-- Define data access beans -->
<!-- Add Services -->
</beans>

Cairngorm Configuration for Flex

Value Objects


value objects are used in Flex to map to object data from Spring domain
objects. A value object is nothing more than a class implementation of a custom data type built

in ActionScript that contains properties to support data visualization in views.









The ClientVO.as Value Object


package com.af.clientmanager.model.vo
{
[Bindable]
[RemoteClass(alias="com.af.core.domain.Client")]
public class ClientVO
{
public var objectIdentifier:Number;
public var assocobjectID:Number;
public var clientName:String;

public var clientID:int;
public var link:String;
public var description:String;
public var notes:String;
public var phone:String;
public var addressLine1:String;
public var addressLine2:String;
public var city:String;
public var state:String;
public var zip:String;
}
}

Cairngorm Delegates


The delegates we create here will eventually house all of the methods that relate to Spring bean operations

The Client Delegate (ClientDelegate.as)


package com.af.clientmanager.business
{
import com.adobe.cairngorm.business.ServiceLocator;
import com.af.model.vo.*;
import mx.rpc.AsyncToken;
import mx.rpc.IResponder;
public class ClientDelegate
{
private var responder : IResponder;
private var service : Object;
public function ClientDelegate( responder : IResponder )
{
this.service = ServiceLocator.getInstance().getService(
"clientService" );
this.responder = responder;
}


// Method calls...
public function insertClient(clientVO : ClientVO ): void
{
var call:AsyncToken = service.insertLoreCategory(ClientVO);
call.addResponder( responder );
}
}
}

Model Locator


The ModelLocator in Cairngorm serves as a singleton class responsible for holding stateful
application information and data returned from data sources. These stateful global classes are accessible from any components in the application that access an instance of the model. This allows you to hold data on the model and bind views to the data. If the data is updated, the view is updated automatically using data binding. You may also elect to create submodels for large applications.









The AF – Client Manager Model Locator (ModelLocator.as)


package com.af.clientmanager.model
{
import com.adobe.cairngorm.model.ModelLocator;
[Bindable]
public class ModelLocator implements com.adobe.cairngorm.model.ModelLocator
{
private static var modelLocator :
com.af.clientmanager.model.ModelLocator;
public static function getInstance() :
com.af.clientmanager.model.ModelLocator
{
if ( modelLocator == null )
modelLocator = new com.af.clientmanager.model.ModelLocator();
return modelLocator;
}

{
if (com.af.clientmanager.model.ModelLocator.modelLocator != null)
throw new Error( "Only one ModelLocator instance should " +
"be instantiated" );
}
// Add all global constants, variables, and submodels here
public const SERVICE_ENDPOINT =
"http://localhost:8080/af_Central/spring/messagebroker/amf";
}
}


Services Definition Class

The services class is a Cairngorm ServiceLocator component that defines all endpoints for
RemoteObject calls to the Spring service layer. Since we are planning to have a separate service layer from the Flex project (with a different web context in Tomcat), we need to define an endpoint on the RemoteObjects. The endpoint tag takes the fully qualified URL of the location the AMF gateway is running on our BlazeDS server, such as the following:

http://localhost:8080/af_Central/spring/messagebroker/amf.

The Services.mxml File with RemoteObject Endpoints



<?xml version="1.0" encoding="utf-8"?>
<cairngorm:ServiceLocator
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:cairngorm="com.adobe.cairngorm.business.*">
<mx:Script>
<![CDATA[
import com.af.clientmanager.model.ModelLocator;
[Bindable]
public var model : ModelLocator = ModelLocator.getInstance();
]]>
</mx:Script>
<mx:RemoteObject endpoint="{model.SERVICE_ENDPOINT}"
id="clientService"
destination="clientService"
showBusyCursor="true">
</mx:RemoteObject>

<mx:RemoteObject endpoint="{model.SERVICE_ENDPOINT}"
id="dashboardService"
destination="dashboardService"
showBusyCursor="true">
</mx:RemoteObject>
<mx:RemoteObject endpoint="{model.SERVICE_ENDPOINT}"
id="projectService"
destination="projectService"
showBusyCursor="true">
</mx:RemoteObject>
</cairngorm:ServiceLocator>


Controller Class Definition

The controller class in Cairngorm will map events to commands. Flex is an event-driven framework, and Cairngorm extends the base event. Every database interaction will require an event and command to execute a transaction based on a user gesture in Flex. The command is responsible for processing the result or fault and moving the data to the appropriate model for a view to be updated

The Main Controller Class (MainController.as)


package com.af.clientmanager.control
{
import com.adobe.cairngorm.control.FrontController;
import com.af.clientmanager.control.commands.events.*;
public class MainController extends FrontController
{
public function MainController():void
{
addCommand(GerClientsEvent.EVENT_GET_CLIENT,
GetClientCommand );
addCommand(DeleteClientEvent.EVENT_DELETE_CLIENT,
DeleteClientCommand );
// Add all command-to-event mappings here...
}
}
}


Service Locator and Front Controller in the Flex Application

In the root MXML component that contains the <mx:Application> tag, we need to set the
Services and MainController instances. These will be loaded into memory when the Flex
application is started and will set listeners for service and CairngormEvent calls






The af_ClientManager.mxml File


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
pageTitle="AF Client Manager"
layout="absolute"
xmlns:view="com.af.view.*"
xmlns:services="com.af.clientmanager.services.*"
xmlns:control="com.af.clientmanager.control.*">
<mx:Style source="assets/css/style.css"/>
<!-- ===================================================================== -->
<!-- the ServiceLocator where we specify the remote services -->
<services:Services id="services"/>
<!-- the FrontController, containing Commands-to-Event mappings -->
<control:MainController id="mainController" />
<!-- ===================================================================== -->
</mx:Application>

Leave a Reply

Subscribe to Posts | Subscribe to Comments

About This Site

Howdy! My name is Suersh Rohan and I am the developer and maintainer of this blog. It mainly consists of my thoughts and opinions on the technologies I learn,use and develop with.

Blog Archive

Powered by Blogger.

- Copyright © My Code Snapshots -Metrominimalist- Powered by Blogger - Designed by Suresh Rohan -