这段时间把Spring-BlazeDs-Intergate这个项目学习了下,主要学的有几大点。
a,运行环境搭建。
b,远程方法的调用
c,消息服务的使用。
1,先说环境的搭建吧。
我的环境是 win7+myeclipse6.5+spring2.5.6+blazds4+Spring-BlazeDs-Intergate1.0.3
第一步:既然说Spring-BlazeDs-Intergate了,那我们的环境最终目的就是让Flex能访问Sping管理的Bean。为了空到这个目的,你要先建立一个j2ee项目,然后把blazds4,和Spring-BlazeDs-Intergate1.0.3的包加入到项目classPath中,这里假设你的后面的spring+hibernat/ibats/其它数据访问层 都已配置好了。
第二步:要修改新建立二个文件,并修改一个文件
新建立的二个文件,分别services-config.xml,它在${web-context}/webapp/WEB-INF/flex目录下,也许你的项目中没有flex目录,那你得自己手动创建一下。
内容为
<?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-secure-amf" class="mx.messaging.channels.SecureAMFChannel">
<endpoint url="https://{server.name}:{server.port}/{context.root}/messagebroker/amfsecure" class="flex.messaging.endpoints.SecureAMFEndpoint"/>
<properties>
<add-no-cache-headers>false</add-no-cache-headers>
</properties>
</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>
<channel-definition id="my-longpolling-amf" class="mx.messaging.channels.AMFChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amflongpolling" class="flex.messaging.endpoints.AMFEndpoint"/>
<properties>
<polling-enabled>true</polling-enabled>
<polling-interval-seconds>5</polling-interval-seconds>
<wait-interval-millis>60000</wait-interval-millis>
<client-wait-interval-millis>1</client-wait-interval-millis>
<max-waiting-poll-requests>200</max-waiting-poll-requests>
</properties>
</channel-definition>
<channel-definition id="my-streaming-amf" class="mx.messaging.channels.StreamingAMFChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/streamingamf" class="flex.messaging.endpoints.StreamingAMFEndpoint"/>
</channel-definition>
</channels>
<security>
<security-constraint id="trusted">
<roles>
<role>ROLE_USER</role>
<role>ROLE_ADMIN</role>
</roles>
</security-constraint>
</security>
<logging>
<target class="flex.messaging.log.ConsoleTarget" level="Warn">
<properties>
<prefix>[BlazeDS] </prefix>
<includeDate>false</includeDate>
<includeTime>false</includeTime>
<includeLevel>false</includeLevel>
<includeCategory>false</includeCategory>
</properties>
<filters>
<pattern>Endpoint.*</pattern>
<pattern>Service.*</pattern>
<pattern>Configuration</pattern>
</filters>
</target>
</logging>
<system>
<redeploy>
<enabled>false</enabled>
</redeploy>
</system>
</services-config>
第二个文件是flex-servlet.xml它在${web-context}/webapp/WEB-INF目录下。内容为
<?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-2.5.xsd
http://www.springframework.org/schema/flex
http://www.springframework.org/schema/flex/spring-flex-1.0.xsd">
<flex:message-broker>
<flex:message-service
default-channels="my-longpolling-amf,my-amf,my-streaming-amf,my-polling-amf" />
</flex:message-broker>
<!-- Expose the productService bean for BlazeDS remoting -->
<flex:remoting-destination ref="roleManager"/>
</beans>
第三个文件就是你的web.xml文件
在保证你spring文件被配置加载成功的情况下,
(我们一般采用如下
<!-- Context Configuration locations for Spring XML files -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/*-spring.xml
</param-value>
</context-param>
<!-- Listeners -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
这种组合来加载)
增加如下配置
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>
<servlet>
<servlet-name>flex</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>flex</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
切记 <servlet-name>要写为flex,因为你刚才新加的第二个文件名称是flex-servlet.xml。
如果不出什么意外的话,做这三步以后,你的就能用flex 的RemoteObject组件来访问SPRING管理的bean了。
还得补充一点,不是flex想访问spring 的bean就能访问的,只有spring把bean暴露给flex,它对能访问。
那下面就接着说我上面提到的第二大点运程服务的调用:
这其实也包括三步,第部分,就是像写好我们的服务类,并配置成spring的bean.
第二步不是把这个bean暴露为remoting-destination.即flex可调用的远程目标。
第三步就是写flex程序调用这个remoting-destination.