使用Spring BlazeDS Integration集成Java和Flex
Spring BlazeDS Integration项目地址http://www.springsource.org/spring-flex,其中的Reference Documentation还没中文版,自己啃了几天,搞出个demo,在这里做个笔记备忘。
1. 准备好以下jar包
Spring |
Hibernate(如果需要的话) |
BlazeDS |
Spring-flex |
2. 先用MyEclipse建立Web工程,再加入Flex。
PS:在工程的属性中Flex Builder Path的源代码,把src目录改成flex_src(src习惯用来放java的源代码)
3. 配置
如果下载了BlazeDS.war,把WEB-INF目录下的flex目录复制到项目的WEB-INF目录。
如果没有下载,手工在WEB-INF建个flex目录,再新建一个services-config.xml文件。添加以下的配置:
<?xml version="1.0" encoding="UTF-8"?>
<services-config>
<services>
<default-channels>
<channel ref="my-amf" />
</default-channels>
</services>
<channels>
<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
<endpoint
url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf"
class="flex.messaging.endpoints.AMFEndpoint" />
</channel-definition>
<channel-definition id="my-polling-amf"
class="mx.messaging.channels.AMFChannel">
<endpoint
url="http://{server.name}:{server.port}/{context.root}/messagebroker/amfpolling"
class="flex.messaging.endpoints.AMFEndpoint" />
<properties>
<polling-enabled>true</polling-enabled>
<polling-interval-seconds>4</polling-interval-seconds>
</properties>
</channel-definition>
</channels>
</services-config>
接下来在web.xml加上:
<servlet>
<servlet-name>flexServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>flexServlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
接下来就是spring的配置了,在WEB-INF下建一个spring目录,用来存放spring的配置文件。
新建一个base.xml文件,引入flex的命名空间
xmlns:flex="http://www.springframework.org/schema/flex"
配置 <flex:message-broker />就会把Spring拦截到的消息交给BlazeDS处理了。
如果要把业务方法暴露给flex调用,只需要在<beans>加上<flex:remoting-destination/>就可以了
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:flex="http://www.springframework.org/schema/flex"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/flex
http://www.springframework.org/schema/flex/spring-flex-1.0.xsd"
default-autowire="byName">
<!-- Flex的AMF协议配置 -->
<flex:message-broker />
<bean id="userService" class="blue.system.service.UserService">
<flex:remoting-destination/>
</bean>
</beans>
4. flex客户端
<mx:RemoteObject id="userSerice" endpoint=http://localhost:8080/demo/messagebroker/amf/>
就可以用userService来调用java端的方法了