blazeds与spring的结合使用

1.什么是spring blazeds integration?



Spring has always aimed to be agnostic to the client technologies being used to access its core services, intentionally leaving options open and letting the community drive the demand for any new first-class integration solutions to be added to the Spring project portfolio. Spring BlazeDS Integration is an answer to the commmunity demand for a top-level solution for building Spring-powered Rich Internet Applications using Adobe Flex for the client-side technology.

BlazeDS is an open source project from Adobe that provides the remoting and messaging foundation for connecting a Flex-based front-end to Java back-end services. Though it has previously been possible to use BlazeDS to connect to Spring-managed services, it has not been in a way that feels "natural" to a Spring developer, requiring the extra burden of having to maintain a separate BlazeDS xml configuration. Spring BlazeDS Integration turns the tables by making the BlazeDS MessageBroker a Spring-managed object, opening up the pathways to a more extensive integration that follows "the Spring way".

2.系统需求。

Java 5 以上

Spring 2.5.6 以上

Adobe BlazeDS 3.2 以上

3.服务端开发。

在服务器端的开发主要涉及了web.xml和applicationContext的配置。

其中web.xml配置如下:定义了spring的分发servlet和其映射的表达式。
<?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>BlazeDS</display-name>
	<description>BlazeDS Application</description>
	<!-- MessageBroker Servlet -->
	<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>
	<servlet-mapping>
		<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
		<url-pattern>/messagebroker/*</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>springblazeds.html</welcome-file>
	</welcome-file-list>
	<login-config>
		<auth-method>BASIC</auth-method>
	</login-config>
	<!-- for WebSphere deployment, please uncomment 
		
		<resource-ref>
		<description>Flex Messaging WorkManager</description>
		<res-ref-name>wm/MessagingWorkManager</res-ref-name>
		<res-type>com.ibm.websphere.asynchbeans.WorkManager</res-type>
		<res-auth>Container</res-auth>
		<res-sharing-scope>Shareable</res-sharing-scope>
		</resource-ref>
	-->
</web-app>

在spring的配置的文件当中,我们需要在头部加上flex的命名空间等:
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-2.5.xsd http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring-flex-1.0.xsd"

接着我们必须定义一个MessageBroker的工厂类和指出service-config.xml的路径。
<bean id="_messageBroker"
		class="org.springframework.flex.core.MessageBrokerFactoryBean">
		<property name="servicesConfigPath"
			value="/WEB-INF/flex/services-config.xml" />
</bean>

如何定义一个映射请求的urlhandler类:
<bean
		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<value>/*=_messageBroker</value>
		</property>
</bean>

再定义一个分发请求的类:
<bean	class="org.springframework.flex.servlet.MessageBrokerHandlerAdapter" />

最后,将我们的服务给暴露出来:
<bean id="productService" class="ProductService">
		
	</bean>
	<bean id="product" class="org.springframework.flex.remoting.RemotingDestinationExporter">
		<property name="messageBroker" ref="_messageBroker"></property>
		<property name="service" ref="productService"></property>
		<property name="channels" value="my-amf"></property>
</bean>

至此服务端开发完毕。

4.客户端开发。

客户端的开发就显得比较简单了:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
	<mx:Script>
		<!--[CDATA[
			import mx.messaging.ChannelSet;
			import mx.messaging.channels.AMFChannel;
			import mx.messaging.Channel;
			import mx.controls.Alert;
			import mx.rpc.events.ResultEvent;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.remoting.RemoteObject;
		
		private var productService:RemoteObject;
		
		private var _productModel:ProductModel;
		
		private var productChannel:Channel;
		private function init():void{
			_productModel=new ProductModel();
			productService=new RemoteObject();
			if(!productChannel){
					productChannel=new AMFChannel("my-amf","http://localhost:8080/springblazeds/messagebroker/amf");
					var channelSet:ChannelSet=new ChannelSet();
					channelSet.addChannel(productChannel);
					productService.channelSet=channelSet;
					productService.destination="product";
			}
		}
		
		private function getProduct(e:MouseEvent):void{
			productService.getProduct();
			productService.addEventListener(FaultEvent.FAULT,getProductFaultHandler);
			productService.addEventListener(ResultEvent.RESULT,getProductResultHandler);
		}
		
		private function getProductFaultHandler(e:FaultEvent):void{
			Alert.show(e.fault.toString());
		}
		
		private function getProductResultHandler(e:ResultEvent):void{
			_productModel=e.result as ProductModel;
			Alert.show(_productModel.productId+" "+_productModel.productName+" "+_productModel.productDetail);			
		}
		]]-->
	</mx:Script>
	<mx:Form verticalCenter="0" horizontalCenter="0" width="400" height="400">
			<mx:Button label="Button" click="getProduct(event)"/>
	</mx:Form>
	
</mx:Application>

你可能感兴趣的:(spring,xml,Web,servlet,Flex)